import axios from 'axios'
import { print } from 'graphql'
import Qieries from '../../../pages/auth/queries'
import * as types from './mutations-types'
import Vue from 'vue'
const login = ({ commit }, {username, password}) => {
axios({
url: 'http://localhost:3000/api',
method: 'post',
data: {
query: print(Qieries.LOGIN),
variables: {
username,
password
}
}
})
.then(data => {
console.log(data)
if (data.data.data.login.success) {
commit(types.LOGIN)
Vue.router.push('/')
}
})
}
const register = ({ commit }, payload) => {
if (payload.success) {
commit(types.LOGIN)
Vue.router.push('/')
}
}
const check = ({commit}) => {
axios({
url: 'http://localhost:3000/api',
method: 'post',
data: {
query: print(Qieries.CHECK)
}
})
.then(data => {
if (data.data.data.isLoggingIn) {
commit(types.LOGIN)
Vue.router.push('/')
}
})
}
export default {
login,
register,
check
}
import passport from 'passport';
import userModel from '../db/models/userSchema'
class ModelIndex {
async login(root, params, context, ast) {
let res = {}
if (typeof context.user === 'undefined') {
context.body.username = params.username;
context.body.password = params.password;
const auth = new Promise((resolve, reject) => {
passport.authenticate('local', (err, user, info) => {
if (err) reject(err)
if (info && info.name && info.name.indexOf('Error') !== -1) reject(new Error(info.message))
if (!user) reject('User not register')
context.logIn(user, (err) => {
if (err) reject(err)
resolve(user)
})
})(context)
})
await auth
.then(data => {
res.success = true
res.message = 'Success'
})
.catch(err => {
res.success = false
res.message = err
})
} else {
res.success = false
res.message = new Error(' Already loggged in ')
}
return res
}
async register(root, params, context, ast) {
let res = {}
const register = new Promise((resolve, reject) => {
console.log(context)
userModel.register(
new userModel({username: params.username}),
params.password,
(err, user) => {
if (err) reject(err)
if (!user) reject(new Error(' No user resolved '))
resolve(user)
}
)
})
await register
.then(data => {
res.success = true
res.message = ' Success '
})
.catch(err => {
if (err) res.message = err; res.success = false
})
return res
}
logout(root, params, context, ast) {
let res = true
if (typeof context.user === 'undefined') {
res = false
}
context.logout()
return res
}
check(root, params, context, ast) {
let res = context.isAuthenticated() ? true : false
return res
}
profile(root, params, context, ast) {
const res = context.user
return res
}
}
export default new ModelIndex()
Отметил httpOnly true даже не думая, что это значит :)