-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added google as oauth provider (#3)
* feat - added google login under provider * feat - added google login under provider * feat - added google as auth provider * chore: simplify gitignore * chore: various improvements --------- Co-authored-by: Akshara Hegde <[email protected]> Co-authored-by: Sébastien Chopin <[email protected]>
- Loading branch information
1 parent
cbc8b7a
commit af4e2d1
Showing
15 changed files
with
209 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = { | ||
root: true, | ||
extends: [ | ||
'@nuxt/eslint-config' | ||
], | ||
rules: { | ||
// Global | ||
semi: ['error', 'never'], | ||
quotes: ['error', 'single'], | ||
'quote-props': ['error', 'as-needed'], | ||
// Vue | ||
'vue/multi-word-component-names': 0, | ||
'vue/max-attributes-per-line': 'off', | ||
'vue/no-v-html': 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
declare module '#auth-utils' { | ||
interface UserSession { | ||
user: { | ||
spotify?: any | ||
github?: any | ||
} | ||
loggedInAt: number | ||
spotify?: any; | ||
github?: any; | ||
google?: any; | ||
}; | ||
loggedInAt: number; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default oauth.googleEventHandler({ | ||
async onSuccess(event, { user }) { | ||
await setUserSession(event, { | ||
user: { | ||
google: user, | ||
}, | ||
loggedInAt: Date.now() | ||
}) | ||
|
||
return sendRedirect(event, '/') | ||
} | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import type { H3Event, H3Error } from 'h3' | ||
import { | ||
eventHandler, | ||
createError, | ||
getQuery, | ||
getRequestURL, | ||
sendRedirect, | ||
} from 'h3' | ||
import { withQuery, parsePath } from 'ufo' | ||
import { ofetch } from 'ofetch' | ||
import { defu } from 'defu' | ||
import { useRuntimeConfig } from '#imports' | ||
|
||
export interface OAuthGoogleConfig { | ||
/** | ||
* Google OAuth Client ID | ||
* @default process.env.NUXT_OAUTH_GOOGLE_CLIENT_ID | ||
*/ | ||
clientId?: string; | ||
|
||
/** | ||
* Google OAuth Client Secret | ||
* @default process.env.NUXT_OAUTH_GOOGLE_CLIENT_SECRET | ||
*/ | ||
clientSecret?: string; | ||
|
||
/** | ||
* Google OAuth Scope | ||
* @default [] | ||
* @see https://developers.google.com/identity/protocols/oauth2/scopes#google-sign-in | ||
* @example ['email', 'openid', 'profile'] | ||
*/ | ||
scope?: string[]; | ||
|
||
/** | ||
* Google OAuth Authorization URL | ||
* @default 'https://accounts.google.com/o/oauth2/v2/auth' | ||
*/ | ||
authorizationURL?: string; | ||
|
||
/** | ||
* Google OAuth Token URL | ||
* @default 'https://oauth2.googleapis.com/token' | ||
*/ | ||
tokenURL?: string; | ||
|
||
/** | ||
* Redirect URL post authenticating via google | ||
* @default '/auth/google' | ||
*/ | ||
redirectUrl: '/auth/google'; | ||
} | ||
|
||
interface OAuthConfig { | ||
config?: OAuthGoogleConfig; | ||
onSuccess: ( | ||
event: H3Event, | ||
result: { user: any; tokens: any } | ||
) => Promise<void> | void; | ||
onError?: (event: H3Event, error: H3Error) => Promise<void> | void; | ||
} | ||
|
||
export function googleEventHandler({ | ||
config, | ||
onSuccess, | ||
onError, | ||
}: OAuthConfig) { | ||
return eventHandler(async (event: H3Event) => { | ||
// @ts-ignore | ||
config = defu(config, useRuntimeConfig(event).oauth?.google, { | ||
authorizationURL: 'https://accounts.google.com/o/oauth2/v2/auth', | ||
tokenURL: 'https://oauth2.googleapis.com/token', | ||
}) as OAuthGoogleConfig | ||
const { code } = getQuery(event) | ||
|
||
if (!config.clientId) { | ||
const error = createError({ | ||
statusCode: 500, | ||
message: 'Missing NUXT_OAUTH_GOOGLE_CLIENT_ID env variables.', | ||
}) | ||
if (!onError) throw error | ||
return onError(event, error) | ||
} | ||
|
||
const redirectUrl = getRequestURL(event).href | ||
if (!code) { | ||
config.scope = config.scope || ['email', 'profile'] | ||
// Redirect to Google Oauth page | ||
return sendRedirect( | ||
event, | ||
withQuery(config.authorizationURL as string, { | ||
response_type: 'code', | ||
client_id: config.clientId, | ||
redirect_uri: redirectUrl, | ||
scope: config.scope.join(' '), | ||
}) | ||
) | ||
} | ||
|
||
const body: any = { | ||
grant_type: 'authorization_code', | ||
redirect_uri: parsePath(redirectUrl).pathname, | ||
client_id: config.clientId, | ||
client_secret: config.clientSecret, | ||
code, | ||
} | ||
const tokens: any = await ofetch(config.tokenURL as string, { | ||
method: 'POST', | ||
body, | ||
}).catch((error) => { | ||
return { error } | ||
}) | ||
if (tokens.error) { | ||
const error = createError({ | ||
statusCode: 401, | ||
message: `Google login failed: ${ | ||
tokens.error?.data?.error_description || 'Unknown error' | ||
}`, | ||
data: tokens, | ||
}) | ||
if (!onError) throw error | ||
return onError(event, error) | ||
} | ||
|
||
const accessToken = tokens.access_token | ||
const user: any = await ofetch( | ||
'https://www.googleapis.com/oauth2/v3/userinfo', | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
} | ||
) | ||
|
||
return onSuccess(event, { | ||
tokens, | ||
user, | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { githubEventHandler } from '../lib/oauth/github' | ||
import { googleEventHandler } from '../lib/oauth/google' | ||
import { spotifyEventHandler } from '../lib/oauth/spotify' | ||
|
||
export const oauth = { | ||
githubEventHandler, | ||
spotifyEventHandler | ||
spotifyEventHandler, | ||
googleEventHandler | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<template> | ||
<div>basic</div> | ||
<div>Nuxt Auth Utils</div> | ||
</template> | ||
|
||
<script setup> | ||
|