mirror of
https://github.com/ArthurDanjou/arthome.git
synced 2026-01-25 09:00:29 +01:00
Working on arthome
This commit is contained in:
@@ -2,17 +2,85 @@ export default oauthGitHubEventHandler({
|
||||
config: {
|
||||
emailRequired: true,
|
||||
},
|
||||
async onSuccess(event, { user }) {
|
||||
await setUserSession(event, {
|
||||
user: {
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
},
|
||||
async onSuccess(event, { user: oauthUser, tokens }) {
|
||||
const userSession = await getUserSession(event)
|
||||
|
||||
// If the user is already signed in, link the account
|
||||
if (userSession?.id) {
|
||||
const user = await findUserById(userSession.id)
|
||||
|
||||
if (user) {
|
||||
await updateUser(userSession.id, {
|
||||
githubId: oauthUser.id,
|
||||
githubToken: tokens.access_token,
|
||||
})
|
||||
|
||||
await setUserSession(event, {
|
||||
id: userSession.id,
|
||||
user: userSession,
|
||||
githubId: oauthUser.id,
|
||||
})
|
||||
|
||||
return sendRedirect(event, '/')
|
||||
}
|
||||
}
|
||||
|
||||
// If the user is not signed in, search for an existing user with that GitHub ID
|
||||
// If it exists, sign in as that user and refresh the token
|
||||
let user = await findUserByGitHubId(oauthUser.id)
|
||||
|
||||
if (user) {
|
||||
await updateUser(user.id, {
|
||||
githubId: oauthUser.id,
|
||||
githubToken: tokens.access_token,
|
||||
})
|
||||
|
||||
await setUserSession(event, {
|
||||
id: user.id,
|
||||
user,
|
||||
})
|
||||
|
||||
return sendRedirect(event, '/')
|
||||
}
|
||||
|
||||
// If the user is not signed in, search for an existing user with that email address without a GitHub ID
|
||||
// If it exists, tells the user to sign in with that account and link the GitHub account
|
||||
user = await findUserBy(
|
||||
and(
|
||||
eq(tables.users.email, oauthUser.email),
|
||||
isNull(tables.users.githubId),
|
||||
),
|
||||
)
|
||||
|
||||
if (user) {
|
||||
await updateSession(event, {
|
||||
password: useRuntimeConfig(event).session.password,
|
||||
}, {
|
||||
message: 'An existing account for this email already exists. Please login and visit your profile settings to add support for GitHub authentication.',
|
||||
})
|
||||
return sendRedirect(event, '/login')
|
||||
}
|
||||
|
||||
// If the user is not signed in and no user exists with that GitHub ID or email address, create a new user
|
||||
const createdUser = await createUser({
|
||||
username: oauthUser.login as string,
|
||||
description: oauthUser.bio as string,
|
||||
name: oauthUser.name as string,
|
||||
email: oauthUser.email as string,
|
||||
avatar: oauthUser.avatar_url as string,
|
||||
githubId: oauthUser.id as number,
|
||||
githubToken: tokens.access_token as string,
|
||||
language: 'en-US',
|
||||
location: 'unknown',
|
||||
private: false,
|
||||
subscription: 'free',
|
||||
})
|
||||
return sendRedirect(event, '/home')
|
||||
},
|
||||
onError(event, error) {
|
||||
console.error('GitHub OAuth error:', error)
|
||||
|
||||
await setUserSession(event, {
|
||||
id: createdUser.id,
|
||||
user: createdUser,
|
||||
})
|
||||
|
||||
return sendRedirect(event, '/')
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user