Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LocalStrategy.js
- import passport from 'passport'
- import Strategy from 'passport-local'
- import { User } from '../../models'
- const localOptions = {
- usernameField: 'email',
- session: true
- }
- passport.serializeUser((user, done) => {
- done(null, user.id)
- })
- passport.deserializeUser((id, done) => {
- User.findById(id).then(user => {
- done(null, user)
- })
- })
- passport.use(
- new Strategy(localOptions, async (email, password, done) => {
- try {
- const user = await User.findOne({ email })
- if (!user) {
- return done(null, false, `No user found for: ${email}`)
- } else if (!user.authenticateUser(password)) {
- return done(null, false, 'Incorrect Password')
- }
- return done(null, user)
- } catch (e) {
- return done(e, false)
- }
- })
- )
- const register = async (args, ctx) => {
- try {
- const existingUser = await User.findOne({ email: args.email })
- if (existingUser) throw new Error('Email already in use!')
- return await User.create({ ...args }).then(
- user =>
- new Promise((resolve, reject) => {
- // req.logIn() method added to req object by `passport`
- ctx.logIn(user, err => {
- if (err) {
- reject(err)
- }
- resolve(user)
- })
- })
- )
- } catch (e) {
- throw e
- }
- }
- const login = async (email, password, ctx) => {
- try {
- return await new Promise((resolve, reject) => {
- passport.authenticate('local', (err, user) => {
- if (!user) reject('Invalid Credentials')
- // req.logIn() method added to req object by `passport`
- ctx.logIn(user, error => {
- if (err) reject(error, 'Shit handle this!')
- resolve(user)
- })
- })({ body: { email, password } })
- })
- } catch (e) {
- throw e
- }
- }
- export { login, register }
- // Resolvers
- import { User } from '../models'
- import { login, register } from '../services'
- export default {
- //...
- Mutation: {
- login: async (parent, { email, password }, ctx) => {
- try {
- return await login(email, password, ctx)
- } catch (e) {
- throw e
- }
- },
- register: async (parent, args, ctx) => {
- try {
- return await register(args, ctx)
- } catch (e) {
- throw e
- }
- }
- }
- }
- // Request Object being passed to GraphQL Middleware
- app.use(
- '/graphql',
- graphqlExpress(req => ({
- schema,
- context: req,
- debug: process.env.NODE_ENV === 'development'
- }))
- // Test Suite
- describe('#login', () => {
- describe('Errors', () => {
- beforeEach(async () => {
- try {
- await cleanup()
- } catch (e) {
- throw e
- }
- })
- test('1. Should throw error if no user found.', async () => {
- try {
- const { errors } = await graphql(
- schema,
- mocks.login('tedmosby@hotmail.com', '123Ab45678')
- )
- expect.assertions(1)
- expect(errors[0]).toHaveProperty('message', 'Invalid Credentials')
- } catch (e) {
- throw e
- }
- })
- })
- describe('Success', () => {
- let testUser
- beforeEach(async () => {
- try {
- await cleanup()
- // Will throw error because `register` fails at `ctx.logIn()`
- const { data: { register } } = await graphql(
- schema,
- mocks.register(
- 'turdFerguson@gmail.com',
- 'Turd',
- 'Ferguson',
- 'abc123A#',
- '316-867-5309'
- )
- )
- testUser = register
- } catch (e) {
- throw e
- }
- })
- test('1. Should return a logged in user.', async () => {
- try {
- // Will throw error because `user` does not exist in database due to above
- // Still will throw error becasue `ctx.logIn()` does not exist without `req` being passed.
- const { data: { login }, errors } = await graphql(
- schema,
- mocks.login(testUser.email, 'abc123A#')
- )
- expect.assertions(6)
- expect(login).toHaveProperty('id')
- expect(login).toHaveProperty('email', 'turdFerguson@gmail.com')
- expect(login).toHaveProperty('firstName', 'Turd')
- expect(login).toHaveProperty('lastName', 'Ferguson')
- expect(login).toHaveProperty('phone', '316-867-5309')
- expect(errors).toEqual(undefined)
- } catch (e) {
- throw e
- }
- })
- })
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement