fetch profile on refresh to get user display name and picture

This commit is contained in:
Raghunandan
2020-06-04 09:56:11 +02:00
parent b68d8ccab1
commit d1e78aa417
3 changed files with 62 additions and 23 deletions
@@ -16,7 +16,43 @@
import express from 'express';
import passport from 'passport';
import { RedirectInfo, RefreshTokenResponse } from './types';
import jwtDecoder from 'jwt-decode';
import { RedirectInfo, RefreshTokenResponse, ProfileInfo } from './types';
export const makeProfileInfo = (
profile: passport.Profile,
params: any,
): ProfileInfo => {
const { provider, displayName: name } = profile;
let email = '';
if (profile.emails) {
const [firstEmail] = profile.emails;
email = firstEmail.value;
}
if (!email && params.id_token) {
try {
const decoded: { email: string } = jwtDecoder(params.id_token);
email = decoded.email;
} catch (e) {
console.error('Failed to parse id token and get profile info');
}
}
let picture = '';
if (profile.photos) {
const [firstPhoto] = profile.photos;
picture = firstPhoto.value;
}
return {
provider,
name,
email,
picture,
};
};
export const executeRedirectStrategy = async (
req: express.Request,
@@ -98,10 +134,22 @@ export const executeRefreshTokenStrategy = async (
),
);
}
resolve({
anyStrategy.userProfile(
accessToken,
params,
});
(err: Error, passportProfile: passport.Profile) => {
if (err) {
reject(err);
}
const profile = makeProfileInfo(passportProfile, params);
resolve({
accessToken,
params,
profile,
});
},
);
},
);
});
@@ -15,7 +15,6 @@
*/
import express from 'express';
import jwtDecoder from 'jwt-decode';
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import {
executeFrameHandlerStrategy,
@@ -77,30 +76,15 @@ export class GoogleAuthProvider implements OAuthProviderHandlers {
}
async refresh(
provider: string,
refreshToken: string,
scope: string,
): Promise<AuthInfoWithProfile> {
const { accessToken, params } = await executeRefreshTokenStrategy(
const { accessToken, params, profile } = await executeRefreshTokenStrategy(
this._strategy,
refreshToken,
scope,
);
// TODO(soapraj): check what happens when you return undefined
let profile;
try {
const { email, name, picture } = jwtDecoder(params.id_token);
profile = {
provider,
email,
name,
picture,
};
} catch (e) {
console.error('Failed to parse id token and get profile info');
}
return {
accessToken,
idToken: params.id_token,
+9 -2
View File
@@ -15,7 +15,6 @@
*/
import express from 'express';
import passport from 'passport';
export type AuthProviderConfig = {
provider: string;
@@ -26,7 +25,7 @@ export type AuthProviderConfig = {
export interface OAuthProviderHandlers {
start(req: express.Request, options: any): Promise<any>;
handler(req: express.Request): Promise<any>;
refresh?(provider: string, refreshToken: string, scope: string): Promise<any>;
refresh?(refreshToken: string, scope: string): Promise<any>;
logout?(): Promise<any>;
}
@@ -78,7 +77,15 @@ export type RedirectInfo = {
status?: number;
};
export type ProfileInfo = {
provider: string;
email: string;
name: string;
picture: string;
};
export type RefreshTokenResponse = {
accessToken: string;
params: any;
profile: ProfileInfo;
};