Add google auth strategy

master
Ian Mancini 4 years ago
parent 01871c2dfc
commit 7b77c76fb2

31
package-lock.json generated

@ -1278,6 +1278,15 @@
"integrity": "sha512-qAfo81CsD7yQIM9mVyh6B/U47li5g7cfpVQEDMfQeF8pSZVwzbhwU3crc0qG4DmpsebpJPR49AKOExQyJ05Cpg==",
"dev": true
},
"@types/oauth": {
"version": "0.9.1",
"resolved": "https://registry.npmjs.org/@types/oauth/-/oauth-0.9.1.tgz",
"integrity": "sha512-a1iY62/a3yhZ7qH7cNUsxoI3U/0Fe9+RnuFrpTKr+0WVOzbKlSLojShCKe20aOD1Sppv+i8Zlq0pLDuTJnwS4A==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/passport": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@types/passport/-/passport-1.0.4.tgz",
@ -1297,6 +1306,28 @@
"@types/passport": "*"
}
},
"@types/passport-google-oauth20": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/@types/passport-google-oauth20/-/passport-google-oauth20-2.0.4.tgz",
"integrity": "sha512-lYLsLzbYKlCopSO1b/FdxsBOgpIIZfvhVdVj5Wgx97udiPDGqv3g8Sq5OsWk20idtPL606SnfPRFgWfx698Nkw==",
"dev": true,
"requires": {
"@types/express": "*",
"@types/passport": "*",
"@types/passport-oauth2": "*"
}
},
"@types/passport-oauth2": {
"version": "1.4.9",
"resolved": "https://registry.npmjs.org/@types/passport-oauth2/-/passport-oauth2-1.4.9.tgz",
"integrity": "sha512-QP0q+NVQOaIu2r0e10QWkiUA0Ya5mOBHRJN0UrI+LolMLOP1/VN4EVIpJ3xVwFo+xqNFRoFvFwJhBvKnk7kpUA==",
"dev": true,
"requires": {
"@types/express": "*",
"@types/oauth": "*",
"@types/passport": "*"
}
},
"@types/passport-twitter": {
"version": "1.0.36",
"resolved": "https://registry.npmjs.org/@types/passport-twitter/-/passport-twitter-1.0.36.tgz",

@ -35,6 +35,7 @@
"@types/node": "^12.11.6",
"@types/passport": "^1.0.4",
"@types/passport-facebook": "^2.1.10",
"@types/passport-google-oauth20": "^2.0.4",
"@types/passport-twitter": "^1.0.36",
"@types/react": "^16.9.11",
"@types/react-dom": "^16.9.4",

@ -1,6 +1,7 @@
import passport from 'passport';
import { facebookStrategy, facebookRouter } from './providers/facebook';
import { twitterStrategy, twitterRouter } from './providers/twitter';
import { googleStrategy, googleRouter } from './providers/google';
import { Router } from 'express'
import UserModel, { User } from '../models/user'
@ -17,8 +18,10 @@ export function initPassport(): void {
passport.use('facebook', facebookStrategy());
passport.use('twitter', twitterStrategy());
passport.use('google', googleStrategy());
}
export const authRouter = Router();
authRouter.use(facebookRouter);
authRouter.use(twitterRouter);
authRouter.use(googleRouter);

@ -0,0 +1,27 @@
import { Strategy, Profile, StrategyOptions, AuthenticateOptionsGoogle } from 'passport-google-oauth20';
import { Router } from 'express'
import passport from 'passport'
import genericStrategy from '../strategy'
const strategyOptions: StrategyOptions = {
clientID: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
callbackURL: '/auth/google/redirect',
}
export function googleStrategy() {
// @ts-ignore: No overload matches this call.
return new Strategy(strategyOptions, genericStrategy<Profile>('google'));
}
export const googleRouter = Router()
const googleAuthenticateOptions: AuthenticateOptionsGoogle = {
scope: ['email', 'profile']
}
googleRouter.get('/google', passport.authenticate('google', googleAuthenticateOptions));
googleRouter.get('/google/redirect',
passport.authenticate('google', { successRedirect: '/museo', failureRedirect: '/' }));

@ -35,7 +35,7 @@ function genericStrategy<ProfileT extends Profile & { _json: any }>(provider: au
name: profile.displayName,
provider,
email: profile.emails?.[0].value,
photo: profile.photos?.[0].value.replace('_normal', ''),
photo: profile.photos?.[0].value.replace('_normal', '').replace('=s96-c', ''),
}).save((err: string) => {
if (err) throw (err);
});

@ -14,7 +14,7 @@
"skipLibCheck": true,
"esModuleInterop": true,
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
"allowSyntheticDefaultImports": true
},
"exclude": [
"node_modules",

@ -9,6 +9,7 @@ const Index: React.FC = () => {
</Head>
<a href="/auth/facebook">Autenticarse con Facebook</a>
<a href="/auth/twitter">Autenticarse con Twitter</a>
<a href="/auth/google">Autenticarse con Google</a>
</>
);
};

Loading…
Cancel
Save