mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 07:03:25 +00:00
feat: removing registration restrictions
Anyone can now register
This commit is contained in:
11
src/lib/user/createUser.ts
Normal file
11
src/lib/user/createUser.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import prisma from "utils/prisma";
|
||||
import { Profile } from "next-auth";
|
||||
|
||||
export default async function createUser(profile: Profile) {
|
||||
return await prisma.user.create({
|
||||
data: {
|
||||
email: profile.email,
|
||||
google_id: profile.sub
|
||||
}
|
||||
});
|
||||
}
|
||||
11
src/lib/user/getUserByProfileProvider.ts
Normal file
11
src/lib/user/getUserByProfileProvider.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Profile } from "next-auth";
|
||||
import prisma from "utils/prisma";
|
||||
|
||||
export default async function getUserByProfileProvider(profile: Profile) {
|
||||
return await prisma.user.findFirst({
|
||||
where: {
|
||||
google_id: profile.sub,
|
||||
email: profile.email
|
||||
}
|
||||
});
|
||||
}
|
||||
15
src/lib/user/updateUser.ts
Normal file
15
src/lib/user/updateUser.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import prisma from "../../utils/prisma";
|
||||
import { Profile } from "next-auth";
|
||||
|
||||
export default async function updateUser(profile: Profile) {
|
||||
return await prisma.user.update({
|
||||
where: {
|
||||
email: profile.email,
|
||||
google_id: profile.sub
|
||||
},
|
||||
data: {
|
||||
email: profile.email,
|
||||
google_id: profile.sub
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,11 +1,18 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import PATHS from "constants/paths";
|
||||
import NextAuth, { NextAuthOptions } from "next-auth";
|
||||
import NextAuth, { NextAuthOptions, Profile } from "next-auth";
|
||||
import GoogleProvider from "next-auth/providers/google";
|
||||
import getUserByProfileProvider from "lib/user/getUserByProfileProvider";
|
||||
import createUser from "lib/user/createUser";
|
||||
import updateUser from "lib/user/updateUser";
|
||||
import prisma from "utils/prisma";
|
||||
|
||||
const authLogger = (profile: Profile, ...args: any[]) => console.log("[AUTH]", profile.email, `(${profile.name} - ${profile.sub})`, ...args);
|
||||
const redirectUser = (errorKey: string) => `${PATHS.LOGIN}?error=${errorKey}`;
|
||||
|
||||
const checkProvider = (provider: string) => provider === "google";
|
||||
const checkAccountDataReceived = (profile: Profile) => !!profile?.sub && !!profile?.email;
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// TODO: refactor auth
|
||||
export const authOptions = {
|
||||
providers: [
|
||||
GoogleProvider({
|
||||
@@ -15,115 +22,52 @@ export const authOptions = {
|
||||
params: {
|
||||
prompt: "consent",
|
||||
access_type: "offline",
|
||||
response_type: "code",
|
||||
},
|
||||
},
|
||||
}),
|
||||
response_type: "code"
|
||||
}
|
||||
}
|
||||
})
|
||||
],
|
||||
callbacks: {
|
||||
async session({ session }) {
|
||||
// check if stored in session still exist in db
|
||||
await prisma.user.findFirstOrThrow({
|
||||
where: { email: session.user.email },
|
||||
const user = await prisma.user.findFirst({
|
||||
where: { email: session.user.email }
|
||||
});
|
||||
return session;
|
||||
return user ? session : undefined;
|
||||
},
|
||||
async signIn({ account: accountParam, profile }) {
|
||||
console.log(
|
||||
"[AUTH]",
|
||||
"User",
|
||||
profile.name,
|
||||
profile.sub,
|
||||
"attempt to log in with",
|
||||
accountParam.provider
|
||||
);
|
||||
if (accountParam.provider !== "google") {
|
||||
console.log("[AUTH]", "User", profile.name, "rejeced : bad provider");
|
||||
return (
|
||||
PATHS.LOGIN +
|
||||
"?error=" +
|
||||
encodeURI("Authentitifcation via Google requise")
|
||||
);
|
||||
if (!checkProvider(accountParam.provider)) {
|
||||
authLogger(profile, "rejected : forbidden provider");
|
||||
return redirectUser("AUTH_REQUIRED");
|
||||
}
|
||||
|
||||
const email = profile?.email;
|
||||
if (email === "") {
|
||||
console.log("[AUTH]", "User", profile.name, "rejeced : missing email");
|
||||
return (
|
||||
PATHS.LOGIN +
|
||||
"?error=" +
|
||||
encodeURI(
|
||||
"Impossible de récupérer l'email associé à ce compte Google"
|
||||
)
|
||||
);
|
||||
}
|
||||
const googleId = profile?.sub;
|
||||
if (googleId === "") {
|
||||
console.log(
|
||||
"[AUTH]",
|
||||
"User",
|
||||
profile.name,
|
||||
"rejeced : missing google id"
|
||||
);
|
||||
return (
|
||||
PATHS.LOGIN +
|
||||
"?error=" +
|
||||
encodeURI(
|
||||
"Impossible de récupérer l'identifiant associé à ce compte Google"
|
||||
)
|
||||
);
|
||||
if (!checkAccountDataReceived(profile)) {
|
||||
authLogger(profile, "rejected : missing data from provider", profile);
|
||||
return redirectUser("MISSING_PROVIDER_VALUES");
|
||||
}
|
||||
|
||||
try {
|
||||
const account = await prisma.user.findFirst({
|
||||
where: {
|
||||
google_id: googleId,
|
||||
email,
|
||||
},
|
||||
});
|
||||
const accountCount = await prisma.user.count();
|
||||
if (!account) {
|
||||
if (accountCount === 0) {
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
email,
|
||||
google_id: googleId,
|
||||
},
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
console.log(
|
||||
"[AUTH]",
|
||||
"User",
|
||||
profile.name,
|
||||
"rejeced : not authorized"
|
||||
);
|
||||
return (
|
||||
PATHS.LOGIN +
|
||||
"?error=" +
|
||||
encodeURI(
|
||||
"Vous n'êtes pas autorisé à vous connecter avec ce compte Google"
|
||||
)
|
||||
);
|
||||
const isUserExists = await getUserByProfileProvider(profile);
|
||||
if (isUserExists) {
|
||||
await updateUser(profile);
|
||||
authLogger(profile, "success : user updated");
|
||||
} else {
|
||||
console.log("[AUTH]", "User", profile.name, "success");
|
||||
return true;
|
||||
await createUser(profile);
|
||||
authLogger(profile, "success : user created");
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.log("[AUTH]", "User", profile.name, "unhandled error");
|
||||
authLogger(profile, "rejected : unhandled error");
|
||||
console.error(error);
|
||||
return (
|
||||
PATHS.LOGIN +
|
||||
"?error=" +
|
||||
encodeURI("Une erreur est survenue lors de l'authentification")
|
||||
);
|
||||
return redirectUser("AUTH_ERROR");
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
pages: {
|
||||
signIn: PATHS.LOGIN,
|
||||
error: PATHS.LOGIN,
|
||||
signOut: PATHS.LOGOUT,
|
||||
},
|
||||
signOut: PATHS.LOGOUT
|
||||
}
|
||||
} as NextAuthOptions;
|
||||
export default NextAuth(authOptions);
|
||||
export default NextAuth(authOptions);
|
||||
2
src/types/i18next.d.ts
vendored
2
src/types/i18next.d.ts
vendored
@@ -18,5 +18,5 @@ declare module "i18next" {
|
||||
}
|
||||
}
|
||||
|
||||
// Ugly hack because of the above declaration, i cant use "t" function params
|
||||
// Ugly hack because of the above declaration, I cant use "t" function params
|
||||
type TFunctionParam = undefined;
|
||||
|
||||
Reference in New Issue
Block a user