Skip to content

Commit

Permalink
ar(temp) auth stuff messy
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloreale committed Jul 13, 2024
1 parent 9ba2eb0 commit 889d4d5
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 36 deletions.
50 changes: 50 additions & 0 deletions lib/auth/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// config.ts
export const authConfig = {
providers,
adapter: PrismaAdapter(PrivatePrisma),
session: {
strategy: 'jwt',
},
events: {},
callbacks: {
async signIn() {
// extra sign-in checks
return true;
},
async redirect() {
return `${process.env.MAIN_URL}`;
},
async jwt({ user, token }) {
if (user) {
// Note that this if condition is needed
token.user = { ...user };
}
return token;
},
async session({ session, token }) {
if (token?.user) {
// Note that this if condition is needed
session.user = token.user;
}
return session;
},
},
cookies: {
pkceCodeVerifier: {
name: 'next-auth.pkce.code_verifier',
options: {
httpOnly: true,
sameSite: 'none',
path: '/',
secure: true,
},
},
},
pages: {
signIn: '/signin',
signOut: '/',
error: '/error', // Error code passed in query string as ?error=
verifyRequest: '/verify', // (used for check email message)
// newUser: '/' // New users will be directed here on first sign in (leave the property out if not of interest)
},
};
72 changes: 37 additions & 35 deletions lib/auth/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import GithubProvider from 'next-auth/providers/github';
import GoogleProvider from 'next-auth/providers/google';
import AppleProvider from 'next-auth/providers/apple';
import FacebookProvider from 'next-auth/providers/facebook';
import EmailProvider from 'next-auth/providers/email';
// import EmailProvider from 'next-auth/providers/email';
import { PrismaAdapter } from '@auth/prisma-adapter';
// import { authConfig } from './config';

const providers = [
EmailProvider({
server: process.env.EMAIL_SERVER as string,
from: process.env.EMAIL_FROM as string,
// maxAge: 24 * 60 * 60, // How long email links are valid for (default 24h)
}),
export const providers = [
// EmailProvider({
// server: process.env.EMAIL_SERVER as string,
// from: process.env.EMAIL_FROM as string,
// // maxAge: 24 * 60 * 60, // How long email links are valid for (default 24h)
// }),
GithubProvider({
clientId: process.env.GITHUB_ID as string,
clientSecret: process.env.GITHUB_SECRET as string,
Expand All @@ -33,21 +34,13 @@ const providers = [
}),
];

export const providerMap = providers.map((provider) => {
if (typeof provider === 'function') {
const providerData = provider();
return { id: providerData.id, name: providerData.name };
} else {
return { id: provider.id, name: provider.name };
}
});

export const authOptions = {
// config.ts
export const authConfig = {
providers,
adapter: PrismaAdapter(PrivatePrisma),
session: {
strategy: 'jwt',
},
// session: {
// strategy: 'jwt',
// },
events: {},
callbacks: {
async signIn() {
Expand All @@ -57,20 +50,20 @@ export const authOptions = {
async redirect() {
return `${process.env.MAIN_URL}`;
},
async jwt({ user, token }) {
if (user) {
// Note that this if condition is needed
token.user = { ...user };
}
return token;
},
async session({ session, token }) {
if (token?.user) {
// Note that this if condition is needed
session.user = token.user;
}
return session;
},
// async jwt({ user, token }) {
// if (user) {
// // Note that this if condition is needed
// token.user = { ...user };
// }
// return token;
// },
// async session({ session, token }) {
// if (token?.user) {
// // Note that this if condition is needed
// session.user = token.user;
// }
// return session;
// },
},
cookies: {
pkceCodeVerifier: {
Expand All @@ -92,4 +85,13 @@ export const authOptions = {
},
};

export const { auth, handlers, signIn, signOut }: AuthOptions = NextAuth(authOptions);
export const providerMap = providers.map((provider) => {
if (typeof provider === 'function') {
const providerData = provider();
return { id: providerData.id, name: providerData.name };
} else {
return { id: provider.id, name: provider.name };
}
});

export const { auth, handlers, signIn, signOut, config }: AuthOptions = NextAuth(authConfig);
2 changes: 1 addition & 1 deletion lib/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// index.ts
export { authOptions, auth, handlers } from './constants';
export { auth, handlers, authConfig } from './constants';
13 changes: 13 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// middleware.ts
import { authConfig } from '@auth';
import NextAuth from 'next-auth';

// Use only one of the two middleware options below
// 1. Use middleware directly
export const { auth: middleware } = NextAuth(authConfig);

// 2. Wrapped middleware option
// const { auth } = NextAuth(config)
// export default auth(async function middleware(req: NextRequest) {
// // Your custom middleware logic goes here
// })
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@constants": ["./lib/types/constants"],
"@constants/*": ["./lib/types/constants/*"],
"@auth": ["./lib/auth"],
"@auth/config": ["./lib/auth/config"],
"@auth/adapter": ["./src/app/api/auth/[...nextauth]"],
"@auth/adapter/*": ["./src/app/api/auth/[...nextauth]/*"],
"@state/*": ["./lib/state/*"],
Expand Down

0 comments on commit 889d4d5

Please sign in to comment.