Skip to content
Snippets Groups Projects
Commit a78fe111 authored by Yusuf Akgül's avatar Yusuf Akgül :hatching_chick:
Browse files

fix github signup

parent 839546b6
No related branches found
No related tags found
No related merge requests found
......@@ -111,6 +111,11 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
}
}
async function onGitHub() {
setIsGitHubLoading(true)
await signIn("github", { callbackUrl: searchParams?.get("from") || "/home" })
}
return (
<div className={cn("grid gap-6", className)} {...props}>
<form onSubmit={handleSubmit(onSubmit)}>
......@@ -221,10 +226,7 @@ export function UserAuthForm({ type, className, ...props }: UserAuthFormProps) {
<Button
variant="outline"
type="button"
onClick={() => {
setIsGitHubLoading(true)
signIn("github", { callbackUrl: searchParams?.get("from") || "/home" })
}}
onClick={onGitHub}
disabled={isLoading || isGitHubLoading}
>
{isGitHubLoading ? (
......
......@@ -65,39 +65,6 @@ export const authOptions: NextAuthOptions = {
],
secret: env.NEXTAUTH_SECRET,
callbacks: {
async signIn({ user, account }) {
if (account?.provider === 'github') {
const { name, email } = user;
if (!name || !email) {
return false;
}
let username = await normalize(name.toLowerCase().replace(/\s/g, ''));
let isUnique = false;
while (!isUnique) {
const existingUserName = await db.user.findFirst({
where: {
username,
NOT: { email },
},
})
if (existingUserName) {
username = `${username}${Math.floor(Math.random() * 1000)}`
} else {
isUnique = true;
}
}
await db.user.update({
where: { email },
data: { username },
});
}
return true;
},
async session({ token, session }) {
if (token) {
session.user.id = token.id
......@@ -123,6 +90,34 @@ export const authOptions: NextAuthOptions = {
return token
}
if (!dbUser.username) {
let username = await normalize(dbUser.name?.toLowerCase().replace(/\s/g, ''));
const email = dbUser.email?.toLowerCase();
let isUnique = false;
while (!isUnique) {
const existingUserName = await db.user.findFirst({
where: {
username,
NOT: { email },
},
});
if (existingUserName) {
username = `${username}${Math.floor(Math.random() * 1000)}`;
} else {
isUnique = true;
}
}
dbUser.username = username;
await db.user.update({
where: { email },
data: { username },
});
}
return {
id: dbUser.id,
name: dbUser.name,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment