-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: ✨ feat a provider for twitch oauth2 * fix: 🎨 define grant_type and storage setters for twitch * docs: 📝 explain tokenLogin why and when * chore: 🔥 set auto redirect to profile Make easier to fetch profile info and auto redirectTo profile after successfuly logged in * fix: 🐛 add missing twitch provider profile data * fix: ⚰️ abort the auto profile idea * refactor: remove dead code on utils * docs: rollback readme changes to standard * feat: ⚡ feat cookies to transport data through storage and plugin * docs: fix duplicated documentation of cookie * fix: remove required redundants and exclusive usage points * fix: remove dead imports and code * fix: rollback redirect params and dead imports * docs: remove cookies declaration on readme * fix: remove useless id on tokenHeaders * fix: remove useless conditional usage * styles: remove lb and missing semicolon * feat: twitch oauth2 provider --------- Co-authored-by: bogeychan <[email protected]>
- Loading branch information
1 parent
975c8e7
commit 7f97fb1
Showing
6 changed files
with
131 additions
and
8 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
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import type { TOAuth2Provider } from '..'; | ||
import { env } from '../utils'; | ||
|
||
/** | ||
* @see https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#authorization-code-grant-flow | ||
*/ | ||
type TTwitchParams = { | ||
/** | ||
* Set to true to force the user to re-authorize your app’s access to their resources. The default is false. | ||
*/ | ||
force_verify?: boolean; | ||
}; | ||
|
||
export type TTwitchToken = { | ||
client_id: string; | ||
login: string; | ||
scopes: string[]; | ||
user_id: string; | ||
expires_in: number; | ||
}; | ||
|
||
export type TTwitchTokenValidationResult = { | ||
status: 200 | 401 | number; | ||
message?: string; | ||
token?: TTwitchToken; | ||
}; | ||
|
||
/** | ||
* @example | ||
* | ||
* const twitchProvider = twitch(); | ||
* | ||
* // ... | ||
* | ||
* const tokenHeaders = await ctx.tokenHeaders('twitch'); | ||
* | ||
* if ((await validateToken(tokenHeaders)).status === 200) { | ||
* const user = await fetch('https://api.twitch.tv/helix/users', { | ||
* headers: { 'Client-Id': twitchProvider.clientId, ...tokenHeaders } | ||
* }); | ||
* } | ||
* | ||
* @see https://dev.twitch.tv/docs/authentication/validate-tokens | ||
*/ | ||
export async function validateToken(headers: { | ||
Authorization: string; | ||
}): Promise<TTwitchTokenValidationResult> { | ||
const response = await fetch('https://id.twitch.tv/oauth2/validate', { | ||
headers | ||
}); | ||
|
||
if (!response.ok) { | ||
throw response; | ||
} | ||
|
||
const isJson = response.headers | ||
.get('Content-Type') | ||
?.startsWith('application/json'); | ||
|
||
if (!isJson) { | ||
throw response; | ||
} | ||
|
||
const json = await response.json(); | ||
|
||
if (response.status === 200) { | ||
return { | ||
status: 200, | ||
token: json | ||
}; | ||
} | ||
|
||
return json; | ||
} | ||
|
||
export function twitch({ force_verify }: TTwitchParams = {}): TOAuth2Provider { | ||
const authParams: TTwitchParams = {}; | ||
|
||
if (typeof force_verify === 'boolean') { | ||
authParams.force_verify = force_verify; | ||
} | ||
|
||
return { | ||
clientId: env('TWITCH_OAUTH_CLIENT_ID'), | ||
clientSecret: env('TWITCH_OAUTH_CLIENT_SECRET'), | ||
|
||
auth: { | ||
url: 'https://id.twitch.tv/oauth2/authorize', | ||
params: authParams | ||
}, | ||
|
||
token: { | ||
url: 'https://id.twitch.tv/oauth2/token', | ||
params: {} | ||
} | ||
}; | ||
} | ||
|
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