Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
callbackURL: domainStr + '/auth/reddit/callback',
state: true
},
Promise.coroutine(function * (token, tokenSecret, profile, done) {
profile.displayName = profile.name;
user = yield settings.createUser(profile, "reddit");
done(null, user);
})
)
);
}
// -- Github
if(typeof config.app.data.passport_github !== "undefined") {
var GitHubStrategy = require('passport-github').Strategy;
passport.use(
new GitHubStrategy(
{
clientID: config.app.data.passport_github.clientId,
clientSecret: config.app.data.passport_github.clientSecret,
callbackURL: domainStr + '/auth/github/callback'
},
Promise.coroutine(function * (accessToken, refreshToken, profile, done) {
console.log(profile);
user = yield settings.createUser(profile, "github");
done(null, user);
})
)
);
}
done(null, createdUser)
} else {
done(null, false)
}
}
}
))
/**
* Facebook strategy of Passport.js
*
* @param
* @returns
*/
const FacebookStrategy = require('passport-facebook').Strategy
passport.use(new FacebookStrategy({
clientID: 'facebook-app-id',
clientSecret: 'facebook-app-secret',
callbackURL: 'http://localhost:' + (process.env.PORT || 3000) + '/users/auth/facebook/callback',
profileFields: ['id', 'displayName', 'name', 'photos', 'email']
},
async (token, tokenSecret, profile, done) => {
// Retrieve user from database, if exists
const user = await User.findOne({
where: {
email: profile.emails[0].value
}
})
if (user) {
done(null, user)
} else {
// If user not exist, create it
});
passport.deserializeUser(async (email, done) => {
try {
const user = await Users.findOne({ email });
// if no user exists then invalidate the previous session
//
if (!user) return done(null, false);
// otherwise continue along
done(null, user);
} catch (err) {
done(err);
}
});
if (config.auth.local) passport.use(Users.createStrategy());
if (config.auth.providers.google)
passport.use(
new GoogleStrategy(
config.auth.strategies.google,
async (accessToken, refreshToken, profile, done) => {
const email = profile.emails[0].value;
try {
let user = await Users.findByEmail(email);
if (user) {
// store the access token and refresh token
if (accessToken) user.set('google_access_token', accessToken);
if (refreshToken) user.set('google_refresh_token', refreshToken);
user = await user.save();
}))
const FacebookStrategy = require('passport-facebook').Strategy
passport.use(new FacebookStrategy({
clientID: 'your-client-id',
clientSecret: 'your-secret',
callbackURL: 'http://localhost:' + (process.env.PORT || 3000) + '/auth/facebook/callback'
},
function(token, tokenSecret, profile, done) {
// retrieve user ...
fetchUser().then(user => done(null, user))
}
))
const TwitterStrategy = require('passport-twitter').Strategy
passport.use(new TwitterStrategy({
consumerKey: 'your-consumer-key',
consumerSecret: 'your-secret',
callbackURL: 'http://localhost:' + (process.env.PORT || 3000) + '/auth/twitter/callback'
},
function(token, tokenSecret, profile, done) {
// retrieve user ...
fetchUser().then(user => done(null, user))
}
))
const GoogleStrategy = require('passport-google-auth').Strategy
passport.use(new GoogleStrategy({
clientId: 'your-client-id',
clientSecret: 'your-secret',
callbackURL: 'http://localhost:' + (process.env.PORT || 3000) + '/auth/google/callback'
},
}
})
} else {
done(null, false)
}
}
))
/**
* google strategy of Passport.js
*
* @param
* @returns
*/
const GoogleStrategy = require('passport-google-auth').Strategy
passport.use(new GoogleStrategy({
clientId: 'your-google-oauth-client-id',
clientSecret: 'your-google-oauth-client-secret',
callbackURL: 'http://localhost:' + (process.env.PORT || 3000) + '/users/auth/google/callback'
},
async (token, tokenSecret, profile, done) => {
// Retrieve user from database, if exists
const user = await User.findOne({
where: {
email: profile.emails[0].value
}
})
if (user) {
done(null, user)
} else {
// If user not exist, create it
const newUser = {
done(null, false)
}
})
})
// serializeUser 在用户登录验证成功以后将会把用户的数据存储到 session 中
passport.serializeUser(function(user, done) {
done(null, user)
})
// deserializeUser 在每次请求的时候将从 session 中读取用户对象
passport.deserializeUser(function(user, done) {
return done(null, user)
})
passport.use(jwtLogin)
passport.use('email-local', localEmailLogin)
passport.use('username-local', localUsernameLogin)
module.exports = passport
import passport from 'koa-passport'
import conifg from 'config'
import db from 'src/server/db'
import co from 'co'
import { isEmpty } from 'lodash'
import debug from 'debug'
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy
const User = db.users
const opts = {}
opts.clientID = conifg.passport.GOOGLE_APP_ID
opts.clientSecret = conifg.passport.GOOGLE_APP_SECRET
opts.callbackURL = conifg.passport.GOOGLE_CALLBACK
export default passport.use(new GoogleStrategy(
opts,
function (accessToken, refreshToken, profile, done) {
co(function *() {
let user
try {
if (isEmpty(profile.emails) || isEmpty(profile.emails[0].value)) {
throw new Error('no emails')
}
debug('dev')('Google profile.emails', profile.emails[0].value)
const email = profile.emails[0].value
if (!isEmpty(profile.displayName)) {
profile.name = profile.displayName
}
Object.keys(strategies).forEach(name => {
passport.use(name, strategies[name]);
});
Object.keys(this.authConfig.oAuth.providers).forEach((key) => {
const provider = this.authConfig.oAuth.providers[key];
const callbackPath = `/auth/oAuthCallback/${key}`;
const serverApiAddress = this.authConfig.oAuth.serverApiAddress;
const callbackURL = serverApiAddress + callbackPath;
const providerConfig = { ...provider.config, callbackURL };
const providerOptions = { scope: ["email"], ...provider.options, session: false };
passport.use(
new provider.strategy(providerConfig, async (accessToken, refreshToken, profile, cb) => {
try {
let email = profile.email || profile._json.email;
if (email == null && profile.emails != null && profile.emails[0] != null && profile.emails[0].value != null) {
email = profile.emails[0].value;
}
if (profile == null || email == null || profile.id == null) {
throw new Error("NotificationEmail or id is missing!");
}
const response = this.createAuthToken(true, email, provider.name, profile.id, provider.tenant, profile);
cb(null, response);
} catch (err) {
this.logger.warn("passport.strategylogin.error", err);
import * as passport from 'koa-passport';
import { Strategy as GitHubStrategy } from 'passport-github2';
const { APPLICATION } = require('../config/config');
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
passport.use(new GitHubStrategy({
clientID: APPLICATION.GITHUB.ID,
clientSecret: APPLICATION.GITHUB.SECRET,
callbackURL: APPLICATION.GITHUB.CALLBACK
}, (accessToken, refreshToken, profile, done) => {
process.nextTick(function () {
return done(null, {
name: profile.displayName
});
});
}));
export default passport;