Skip to content

Commit

Permalink
cleanup auth.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulTandon1 committed Dec 4, 2024
1 parent cbfb2ba commit 4a114ad
Showing 1 changed file with 37 additions and 72 deletions.
109 changes: 37 additions & 72 deletions auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,78 @@ import { PrismaAdapter } from '@auth/prisma-adapter';

import { prisma } from './app/api/prisma';

console.log('auth running');

// Custom Google Profile Type
// interface GoogleProfile {
// sub: string;
// name: string;
// email: string;
// picture: string;
// given_name: string;
// family_name: string;
// }

// export const authOptions: NextAuthConfig = {
export const { auth, handlers, signIn, signOut } = NextAuth({
providers: [
Google({
clientId: process.env.AUTH_GOOGLE_ID as string,
clientSecret: process.env.AUTH_GOOGLE_SECRET as string,

// temp rahul commentary: overriding default authorization scopes
// overriding default authorization scopes
authorization: {
params: {
scope: 'openid email profile',
},
},

// what I return will be used to create the user object in the database
// profile(profile: GoogleProfile) {
// return {
// id: profile.sub,
// name: profile.name,
// email: profile.email,
// picture: profile.picture,
// firstName: profile.given_name,
// lastName: profile.family_name,
// };
// },
}),
],

adapter: PrismaAdapter(prisma),

// debug: true,

callbacks: {
async signIn({ account, profile }) {
console.log('Account: ', account);
console.log('Profile: ', profile);
// const googleProfile = profile as any;
if (account?.provider !== 'google' || !profile) {
console.error('');
// ensure that the provider is google and profile is defined
// else throw error
if (!account || account.provider !== 'google' || !profile) {
console.error('Account not defined or Account provider is not Google');
return false;
}
// if I'm here, I know that the provider is google and profile is defined
// we can safely assert that provider is Google and profile is defined
const googleProfile = profile as GoogleProfile;
const firstName = googleProfile.given_name || '';
const lastName = googleProfile.family_name || '';

try {
// find user if it exists
let existingUser = await prisma.user.findUnique({
where: { email: googleProfile.email },
});
const isExistingUser =
// using abstract != instead of !== on purpose
null !=
(await prisma.user.findUnique({
where: { email: googleProfile.email },
}));

/**
* TODOs
* abstraction & ease of reading
* ? in typescript.
* user vs account stuff
*/
if (isExistingUser) return true;

// if user does not exist, create user and account.
if (!existingUser) {
existingUser = await prisma.user.create({
data: {
authId: account.providerAccountId,
email: googleProfile.email,
firstName,
lastName,
},
});
// Since user does not exist,
// create new User & Account records in DB
// then return true.
const newUser = await prisma.user.create({
data: {
authId: account.providerAccountId,
email: googleProfile.email,
firstName,
lastName,
},
});

await prisma.account.create({
data: {
userId: existingUser.id,
provider: account.provider!,
providerAccountId: account.providerAccountId!,
refreshToken: account.refresh_token,
accessToken: account.access_token,
accessTokenExpires: account.expires_at
? new Date(account.expires_at * 1000)
: null,
providerType: 'oauth',
},
});
}
await prisma.account.create({
data: {
userId: newUser.id,
provider: account.provider!,
providerAccountId: account.providerAccountId!,
refreshToken: account.refresh_token,
accessToken: account.access_token,
accessTokenExpires: account.expires_at
? new Date(account.expires_at * 1000)
: null,
providerType: 'oauth',
},
});

return true;
} catch (error) {
console.error('Error during sign-in:', error);
return false;
}
// }
return false;
},
},
});

// export default NextAuth(authOptions);
// export const { auth, handlers, signIn, signOut } = NextAuth(authOptions)

0 comments on commit 4a114ad

Please sign in to comment.