init crater

This commit is contained in:
Mohit Panjwani
2019-11-11 12:16:00 +05:30
commit bdf2ba51d6
668 changed files with 158503 additions and 0 deletions

View File

@ -0,0 +1,77 @@
import Ls from '@/services/ls'
import * as types from './mutation-types'
import * as userTypes from '../user/mutation-types'
import * as rootTypes from '../../mutation-types'
import router from '@/router.js'
export const login = ({ commit, dispatch, state }, data) => {
let loginData = {
username: data.email,
password: data.password
}
return new Promise((resolve, reject) => {
axios.post('/api/auth/login', loginData).then((response) => {
let token = response.data.access_token
Ls.set('auth.token', token)
commit('user/' + userTypes.RESET_CURRENT_USER, null, { root: true })
commit(rootTypes.UPDATE_APP_LOADING_STATUS, false, { root: true })
commit(types.AUTH_SUCCESS, token)
window.toastr['success']('Login Successful')
resolve(response)
}).catch(err => {
if (err.response.data.error === 'invalid_credentials') {
window.toastr['error']('Invalid Credentials')
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', err.message)
}
commit(types.AUTH_ERROR, err.response)
Ls.remove('auth.token')
reject(err)
})
})
}
export const refreshToken = ({ commit, dispatch, state }) => {
return new Promise((resolve, reject) => {
let data = {
token: Ls.get('auth.token')
}
console.log('REFRESH ACTION')
axios.post('/api/auth/refresh_token', data).then((response) => {
let token = response.data.data.token
Ls.set('auth.token', token)
commit(types.REFRESH_SUCCESS, token)
resolve(response)
}).catch(err => {
reject(err)
})
})
}
export const logout = ({ commit, dispatch, state }, noRequest = false) => {
if (noRequest) {
commit(types.AUTH_LOGOUT)
Ls.remove('auth.token')
router.push('/login')
return true
}
return new Promise((resolve, reject) => {
axios.get('/api/auth/logout').then((response) => {
commit(types.AUTH_LOGOUT)
Ls.remove('auth.token')
router.push('/login')
window.toastr['success']('Logged out!', 'Success')
}).catch(err => {
reject(err)
commit(types.AUTH_LOGOUT)
Ls.remove('auth.token')
router.push('/login')
})
})
}

View File

@ -0,0 +1,2 @@
export const isAuthenticated = (state) => !!state.token
export const authStatus = (state) => state.status

View File

@ -0,0 +1,23 @@
import mutations from './mutations'
import * as actions from './actions'
import * as getters from './getters'
import Ls from '@/services/ls'
const initialState = {
token: Ls.get('auth.token'),
status: '',
validateTokenError: '',
validateTokenSuccess: ''
}
export default {
namespaced: true,
state: initialState,
getters: getters,
actions: actions,
mutations: mutations
}

View File

@ -0,0 +1,4 @@
export const AUTH_SUCCESS = 'AUTH_SUCCESS'
export const AUTH_LOGOUT = 'AUTH_LOGOUT'
export const AUTH_ERROR = 'AUTH_ERROR'
export const REFRESH_SUCCESS = 'REFRESH_SUCCESS'

View File

@ -0,0 +1,22 @@
import * as types from './mutation-types'
export default {
[types.AUTH_SUCCESS] (state, token) {
state.token = token
state.status = 'success'
},
[types.AUTH_LOGOUT] (state) {
state.token = null
},
[types.AUTH_ERROR] (state, errorResponse) {
state.token = null
state.status = 'error'
},
[types.REFRESH_SUCCESS] (state, token) {
state.token = token
state.status = 'success'
}
}