Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions __tests__/middleware.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
jest.mock('next-auth/middleware', () => ({}))

import { config } from '../middleware'
import { SETTINGS_ACCOUNT_PATH } from '../constants/index.ts'

describe('Main Middleware', () => {
test('expect config to match correct paths', () => {
expect(config.matcher).toStrictEqual([SETTINGS_ACCOUNT_PATH])
})
})
5 changes: 4 additions & 1 deletion __tests__/pages/api/auth/[...nextauth].test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

jest.mock('next-auth')
jest.mock('../../../../helpers/nextAuth.ts')
import { LOGIN_PATH } from '../../../../constants'
import { providers, signIn, jwt, session } from '../../../../helpers/nextAuth'
import nextAuthMiddleware from '../../../../pages/api/auth/[...nextauth]'
import NextAuth from 'next-auth'
Expand All @@ -26,7 +27,9 @@ describe('next-auth middleware', () => {
jwt,
session
},
secret: process.env.SESSION_SECRET
pages: {
signIn: LOGIN_PATH
}
}

res.status.mockReturnValue(res)
Expand Down
1 change: 1 addition & 0 deletions constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const DISCORD_PATH = 'https://discord.gg/c0d3'
export const LOGIN_PATH = '/login'
export const SIGNUP_PATH = '/signup'
export const EXERCISES_PATH = '/exercises'
export const SETTINGS_ACCOUNT_PATH = '/settings/account'

// Color
export const PRIMARY_COLOR_HEX = '#5440d8'
Expand Down
5 changes: 5 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { SETTINGS_ACCOUNT_PATH } from './constants'

export { default } from 'next-auth/middleware'

export const config = { matcher: [SETTINGS_ACCOUNT_PATH] }
5 changes: 4 additions & 1 deletion pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NextApiResponse } from 'next'
import { LoggedRequest } from '../../../@types/helpers'
import { Request, Response } from 'express'
import { signIn, providers, jwt, session } from '../../../helpers/nextAuth'
import { LOGIN_PATH } from '../../../constants'

export default (
req: LoggedRequest & Request,
Expand All @@ -15,5 +16,7 @@ export default (
jwt,
session
},
secret: process.env.SESSION_SECRET
pages: {
signIn: LOGIN_PATH
}
})
12 changes: 8 additions & 4 deletions pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { signIn, SignInResponse } from 'next-auth/react'
import Image from 'next/image'
import { withGetApp, GetAppProps } from '../graphql'
import AlreadyLoggedIn from '../components/AlreadyLoggedIn'
import { CURRICULUM_PATH } from '../constants'

type Values = {
username: string
Expand All @@ -38,7 +39,7 @@ const initialValues = {
}

const ErrorMessages: React.FC<ErrorDisplayProps> = ({ loginErrors }) => {
if (!loginErrors || !loginErrors.length) return <></>
if (!loginErrors?.length) return <></>
const errorMessages = loginErrors.map((message, idx) => {
return <Alert key={idx} alert={{ id: -1, text: message, type: 'urgent' }} />
})
Expand Down Expand Up @@ -99,7 +100,7 @@ export const Login: React.FC<LoginFormProps> = ({
</div>
</Form>
</Formik>
<div className={`d-grid mb-3`}>
<div className="d-grid mb-3">
<div className={styles.orContainer}>
<hr className={styles.lineLeft} />
<span>OR</span>
Expand Down Expand Up @@ -153,8 +154,11 @@ const LoginPage: React.FC<GetAppProps> & WithLayout = ({

if (ok && !error) {
window.localStorage.setItem('loggedIn', 'true')
const { next } = router.query
router.push(next ? (next as string) : '/curriculum')
const { next, callbackUrl } = router.query
const path = next || callbackUrl
const redirectTo = path || CURRICULUM_PATH

router.push(redirectTo as string)
}

setIsLoading(false)
Expand Down