Skip to content

Commit

Permalink
Update auth
Browse files Browse the repository at this point in the history
  • Loading branch information
salimi-my committed May 6, 2024
1 parent 79c5a67 commit 7087233
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"prettier.jsxSingleQuote": true,
"prettier.singleQuote": true,
"prettier.trailingComma": "none"
}
3 changes: 2 additions & 1 deletion app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { GET, POST } from '@/auth';
import { handlers } from '@/auth';
export const { GET, POST } = handlers;
18 changes: 6 additions & 12 deletions auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import prismadb from '@/lib/prismadb';
import authConfig from '@/auth.config';
import { getUserById } from '@/data/user';

export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
update
} = NextAuth({
export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({
pages: {
signIn: '/auth/sign-in',
error: '/auth/sign-in'
Expand All @@ -24,7 +18,7 @@ export const {

if (session.user) {
session.user.name = token.name;
session.user.email = token.email;
session.user.email = token.email as string;
}

return session;
Expand All @@ -46,9 +40,9 @@ export const {
return token;
}
},
adapter: PrismaAdapter(prismadb),
debug: process.env.NODE_ENV !== 'production',
secret: process.env.AUTH_SECRET,
...authConfig,
session: { strategy: 'jwt' },
...authConfig
secret: process.env.AUTH_SECRET,
adapter: PrismaAdapter(prismadb),
debug: process.env.NODE_ENV !== 'production'
});
21 changes: 11 additions & 10 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import NextAuth from 'next-auth';

import authConfig from '@/auth.config';
import { auth } from '@/auth';
import {
authRoutes,
publicRoutes,
apiAuthPrefix,
DEFAULT_SIGNIN_REDIRECT
} from '@/routes';

export const { auth } = NextAuth(authConfig);

export default auth((req) => {
const { nextUrl } = req;
const isSignedIn = !!req.auth;
Expand All @@ -19,15 +15,17 @@ export default auth((req) => {
const isAuthRoute = authRoutes.includes(nextUrl.pathname);

if (isApiAuthRoute || isPublicRoute) {
return null;
return;
}

if (isAuthRoute) {
if (isSignedIn) {
return Response.redirect(new URL(DEFAULT_SIGNIN_REDIRECT, nextUrl));
return Response.redirect(
new URL(DEFAULT_SIGNIN_REDIRECT, nextUrl).toString()
);
}

return null;
return;
}

if (!isSignedIn && !isPublicRoute) {
Expand All @@ -39,11 +37,14 @@ export default auth((req) => {
const encodedCallbackUrl = encodeURIComponent(callbackUrl);

return Response.redirect(
new URL(`/auth/sign-in?callbackUrl=${encodedCallbackUrl}`, nextUrl)
new URL(
`/auth/sign-in?callbackUrl=${encodedCallbackUrl}`,
nextUrl
).toString()
);
}

return null;
return;
});

export const config = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"framer-motion": "^11.1.7",
"lucide-react": "^0.321.0",
"next": "^14.2.3",
"next-auth": "5.0.0-beta.4",
"next-auth": "5.0.0-beta.17",
"next-themes": "^0.3.0",
"plaiceholder": "^3.0.0",
"react": "^18.3.1",
Expand Down
35 changes: 12 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7087233

Please sign in to comment.