chatgpt - auth service test

mail@pastecode.io avatar
unknown
javascript
2 years ago
2.5 kB
4
Indexable
Never
const express = require('express');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const FacebookStrategy = require('passport-facebook').Strategy;
const TwitterStrategy = require('passport-twitter').Strategy;

// create the express app
const app = express();

// configure the Local strategy with your email and password credentials
passport.use(new LocalStrategy({
  usernameField: 'email',
  passwordField: 'password'
}, (email, password, cb) => {
  // retrieve the user with the given email and password
  User.findOne({ email, password }, (err, user) => {
    if (err) { return cb(err); }
    if (!user) { return cb(null, false); }
    return cb(null, user);
  });
}));

// configure the Google Login strategy with your credentials
passport.use(new GoogleStrategy({
  clientID: '<your-client-id>',
  clientSecret: '<your-client-secret>',
  callbackURL: 'http://localhost:3000/auth/google/callback'
}, (accessToken, refreshToken, profile, cb) => {
  // retrieve the user's profile information
  const user = {
    id: profile.id,
    name: profile.displayName,
    email: profile.emails[0].value
  };

  // return the user object
  return cb(null, user);
}));

// configure the Facebook Login strategy with your credentials
passport.use(new FacebookStrategy({
  clientID: '<your-client-id>',
  clientSecret: '<your-client-secret>',
  callbackURL: 'http://localhost:3000/auth/facebook/callback'
}, (accessToken, refreshToken, profile, cb) => {
  // retrieve the user's profile information
  const user = {
    id: profile.id,
    name: profile.displayName,
    email: profile.emails[0].value
  };

  // return the user object
  return cb(null, user);
}));

// configure the Twitter Login strategy with your credentials
passport.use(new TwitterStrategy({
  consumerKey: '<your-consumer-key>',
  consumerSecret: '<your-consumer-secret>',
  callbackURL: 'http://localhost:3000/auth/twitter/callback'
}, (token, tokenSecret, profile, cb) => {
  // retrieve the user's profile information
  const user = {
    id: profile.id,
    name: profile.displayName,
    email: profile.emails[0].value
  };

  // return the user object
  return cb(null, user);
}));

// configure passport to serialize and deserialize user objects to and from the session
passport.serializeUser((user, cb) => {
  // store the user's ID in the session
  cb(null, user