-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added google as oauth provider #3
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why ignoring this?