@coji/remix-auth-google
Version:
<!-- Description -->
69 lines (68 loc) • 2.37 kB
JavaScript
import { OAuth2Strategy } from 'remix-auth-oauth2';
export const GoogleStrategyDefaultScopes = [
'openid',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
];
export const GoogleStrategyDefaultName = 'google';
const userInfoURL = 'https://www.googleapis.com/oauth2/v3/userinfo';
export class GoogleStrategy extends OAuth2Strategy {
name = GoogleStrategyDefaultName;
accessType;
prompt;
includeGrantedScopes;
hd;
loginHint;
constructor({ clientId, clientSecret, redirectURI, scopes, accessType, includeGrantedScopes, prompt, hd, loginHint, }, verify) {
super({
clientId,
clientSecret,
redirectURI,
authorizationEndpoint: 'https://accounts.google.com/o/oauth2/v2/auth',
tokenEndpoint: 'https://oauth2.googleapis.com/token',
scopes: scopes ?? GoogleStrategyDefaultScopes,
}, verify);
this.accessType = accessType ?? 'online';
this.includeGrantedScopes = includeGrantedScopes ?? false;
this.prompt = prompt;
this.hd = hd;
this.loginHint = loginHint;
}
authorizationParams(params, request) {
params.set('access_type', this.accessType);
params.set('include_granted_scopes', String(this.includeGrantedScopes));
if (this.prompt) {
params.set('prompt', this.prompt);
}
if (this.hd) {
params.set('hd', this.hd);
}
if (this.loginHint) {
params.set('login_hint', this.loginHint);
}
return params;
}
static async userProfile(tokens) {
const response = await fetch(userInfoURL, {
headers: {
Authorization: `Bearer ${tokens.accessToken()}`,
},
});
if (!response.ok) {
throw new Error(`Failed to fetch user profile: ${response.statusText}`);
}
const raw = await response.json();
const profile = {
id: raw.sub,
displayName: raw.name,
name: {
familyName: raw.family_name,
givenName: raw.given_name,
},
emails: [{ value: raw.email }],
photos: [{ value: raw.picture }],
_json: raw,
};
return profile;
}
}