Skip to content

Commit

Permalink
chore: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Nov 9, 2023
1 parent fb0fdcf commit 6543730
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Supported providers:
- GitHub
- Spotify
- Google
- Twitch

You can add your favorite provider by creating a new file in [src/runtime/server/lib/oauth/](./src/runtime/server/lib/oauth/).

Expand All @@ -158,6 +159,9 @@ Example: `~/server/routes/auth/github.get.ts`

```ts
export default oauth.githubEventHandler({
config: {
emailRequired: true
},
async onSuccess(event, { user, tokens }) {
await setUserSession(event, {
user: {
Expand Down
3 changes: 3 additions & 0 deletions playground/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ NUXT_OAUTH_SPOTIFY_CLIENT_SECRET=
# Google OAuth
NUXT_OAUTH_GOOGLE_CLIENT_ID=
NUXT_OAUTH_GOOGLE_CLIENT_SECRET=
# Twitch OAuth
NUXT_OAUTH_TWITCH_CLIENT_ID=
NUXT_OAUTH_TWITCH_CLIENT_SECRET=
6 changes: 4 additions & 2 deletions playground/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script setup>
<script setup lang="ts">
const { session } = useUserSession()
</script>

<template>
<pre>{{ session }}</pre>
<UPageBody>
<pre>{{ session }}</pre>
</UPageBody>
</template>
5 changes: 4 additions & 1 deletion playground/server/routes/auth/twitch.get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default oauth.twitchEventHandler({
config: {
emailRequired: true,
},
async onSuccess(event, { user }) {
await setUserSession(event, {
user: {
Expand All @@ -9,4 +12,4 @@ export default oauth.twitchEventHandler({

return sendRedirect(event, '/')
}
})
})
29 changes: 25 additions & 4 deletions src/runtime/server/lib/oauth/twitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export interface OAuthTwitchConfig {
*/
scope?: string[]

/**
* Require email from user, adds the ['user:read:email'] scope if not present
* @default false
*/
emailRequired?: boolean

/**
* Twitch OAuth Authorization URL
* @default 'https://id.twitch.tv/oauth2/authorize'
Expand Down Expand Up @@ -65,7 +71,10 @@ export function twitchEventHandler({ config, onSuccess, onError }: OAuthConfig)

const redirectUrl = getRequestURL(event).href
if (!code) {
config.scope = config.scope || ['user:read:email']
config.scope = config.scope || []
if (config.emailRequired && !config.scope.includes('user:read:email')) {
config.scope.push('user:read:email')
}
// Redirect to Twitch Oauth page
return sendRedirect(
event,
Expand Down Expand Up @@ -107,16 +116,28 @@ export function twitchEventHandler({ config, onSuccess, onError }: OAuthConfig)
}

const accessToken = tokens.access_token
const user: any = await ofetch('https://api.twitch.tv/helix/users', {
const users: any = await ofetch('https://api.twitch.tv/helix/users', {
headers: {
"Client-ID": config.clientId,
'Client-ID': config.clientId,
Authorization: `Bearer ${accessToken}`
}
})

const user = users.data?.[0]

if (!user) {
const error = createError({
statusCode: 500,
message: 'Could not get Twitch user',
data: tokens
})
if (!onError) throw error
return onError(event, error)
}

return onSuccess(event, {
tokens,
user
})
})
}
}

0 comments on commit 6543730

Please sign in to comment.