auth using google

This commit is contained in:
2018-07-07 18:26:59 +03:00
parent aa6b0fb7af
commit bd957c89ef
17 changed files with 468 additions and 216 deletions

View File

@@ -2,6 +2,8 @@ const mongoose = require('mongoose');
const passportLocalMongoose = require('passport-local-mongoose');
const jwt = require('jsonwebtoken');
const uniqueValidator = require('mongoose-unique-validator');
const findOrCreate = require('mongoose-findorcreate');
const { BadRequestError } = require('../errors');
const { secret } = require('../config');
@@ -10,13 +12,17 @@ const { Schema } = mongoose;
const UserSchema = Schema({
username: {
type: String,
required: true,
unique: true,
validate: /^\S*$/,
minLength: 3,
maxLength: 50,
trim: true,
},
googleId: {
type: String,
unique: true,
sparse: true,
},
lists: [{ type: Schema.Types.ObjectId, ref: 'TodoList' }],
todos: [{ type: Schema.Types.ObjectId, ref: 'Todo' }],
});
@@ -26,6 +32,13 @@ UserSchema.plugin(passportLocalMongoose, {
maxAttempts: 20,
});
UserSchema.plugin(uniqueValidator);
UserSchema.plugin(findOrCreate);
UserSchema.pre('validate', async function() {
if (!this.username && !this.googleId) {
throw new BadRequestError('username is required');
}
});
UserSchema.pre('remove', async function() {
await this.model('TodoList')