mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-22 06:49:35 +00:00
Initial commit
This commit is contained in:
commit
d76f4258f7
231 files changed
+27803
No files matched your search
@@ -0,0 +1,48 @@
|
||||
# W / Auth
|
||||
- This plugin is used for managing logged user, and will automatically inject auth store to your main store
|
||||
|
||||
# Required third-party plugins
|
||||
- `npm i vuex@4 && npm i axios-auth-refresh`
|
||||
# Required internal plugins
|
||||
- w/axios
|
||||
|
||||
# Installation
|
||||
1. Create store.ts in `@/store.ts` and initialize vuex store
|
||||
2. Import `@/store.ts` in `main.ts`
|
||||
3. Import w/auth in `main.ts`
|
||||
|
||||
# Installation example
|
||||
- main.ts
|
||||
```javascript
|
||||
import store from '@/store'
|
||||
import auth from '@/plugins/w/auth'
|
||||
|
||||
const app = createApp(App)
|
||||
.use(store)
|
||||
.use(auth)
|
||||
```
|
||||
- store.ts
|
||||
```javascript
|
||||
import { createStore } from 'vuex'
|
||||
|
||||
export default createStore({
|
||||
modules: {}
|
||||
})
|
||||
```
|
||||
# Store actions
|
||||
```javascript
|
||||
this.$store.dispatch('auth/login', credentials)
|
||||
this.$store.dispatch('auth/signup', credentials)
|
||||
this.$store.dispatch('auth/logout')
|
||||
this.$store.dispatch('auth/forgotPassword', credentials)
|
||||
this.$store.dispatch('auth/resetPassword', credentials)
|
||||
this.$store.dispatch('auth/refreshToken')
|
||||
this.$store.dispatch('auth/userInfo')
|
||||
this.$store.dispatch('auth/updateUserInfo', user)
|
||||
```
|
||||
|
||||
# Store getters
|
||||
```javascript
|
||||
this.$store.getters['auth/isLoggedIn']
|
||||
this.$store.getters['auth/user']
|
||||
```
|
||||
@@ -0,0 +1,173 @@
|
||||
import axios from '@/plugins/w/axios/models/axios'
|
||||
import './refreshExpired'
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state: {
|
||||
status: '',
|
||||
_token: localStorage.getItem('wzo_vue_user_token') || '',
|
||||
user: JSON.parse(localStorage.getItem('wzo_vue_user_user') || '{}'),
|
||||
},
|
||||
mutations: {
|
||||
auth_request(state: any) {
|
||||
state.status = 'loading'
|
||||
},
|
||||
auth_success(state: any, data: any) {
|
||||
state.status = 'success'
|
||||
if (data.token) {
|
||||
state._token = data.token
|
||||
localStorage.setItem('wzo_vue_user_token', data.token)
|
||||
}
|
||||
|
||||
if (data.user) {
|
||||
state.user = data.user
|
||||
localStorage.setItem('wzo_vue_user_user', JSON.stringify(data.user))
|
||||
}
|
||||
},
|
||||
auth_error(state: any) {
|
||||
state.status = 'error'
|
||||
state._token = ''
|
||||
localStorage.removeItem('wzo_vue_user_token')
|
||||
localStorage.removeItem('wzo_vue_user_user')
|
||||
},
|
||||
logout(state: any) {
|
||||
state.status = ''
|
||||
state._token = ''
|
||||
state.user = null
|
||||
localStorage.removeItem('wzo_vue_user_token')
|
||||
localStorage.removeItem('wzo_vue_user_user')
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
login({commit}: {commit: any}, credentials: any) {
|
||||
return new Promise((resolve, reject) => {
|
||||
commit('auth_request')
|
||||
|
||||
axios.post_data('v1/auth/login', credentials)
|
||||
.then((response: any) => {
|
||||
const token = response.token
|
||||
const user = response.user
|
||||
|
||||
commit('auth_success', {
|
||||
token: token,
|
||||
user: user
|
||||
})
|
||||
|
||||
resolve(response)
|
||||
}).catch(error => {
|
||||
commit('auth_error')
|
||||
reject(error)
|
||||
})
|
||||
})
|
||||
},
|
||||
signup({commit }: {commit: any}, credentials: any) {
|
||||
return new Promise((resolve, reject) => {
|
||||
commit('auth_request')
|
||||
|
||||
axios.post_data('v1/auth/signup', credentials)
|
||||
.then((response: any) => {
|
||||
const token = response.token
|
||||
const user = response.user
|
||||
|
||||
commit('auth_success', {
|
||||
token: token,
|
||||
user: user
|
||||
})
|
||||
|
||||
resolve(response)
|
||||
}).catch(error => {
|
||||
commit('auth_error')
|
||||
reject(error)
|
||||
})
|
||||
})
|
||||
},
|
||||
logout({commit}: {commit: any}) {
|
||||
return new Promise((resolve) => {
|
||||
axios.post_auth_data('v1/auth/invalidate')
|
||||
.finally(() => {
|
||||
commit('logout')
|
||||
resolve(null)
|
||||
})
|
||||
})
|
||||
},
|
||||
forgotPassword(credentials: any) {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios.post_auth_data('v1/auth/forgot', credentials)
|
||||
.then(response => resolve(response))
|
||||
.catch(error => reject(error))
|
||||
})
|
||||
},
|
||||
resetPassword(credentials: any) {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios.post_auth_data('v1/auth/reset', credentials)
|
||||
.then(response => resolve(response))
|
||||
.catch(error => reject(error))
|
||||
})
|
||||
},
|
||||
async refreshToken({commit}: {commit: any}) {
|
||||
try {
|
||||
const response: any = await axios.post_auth_data('v1/auth/refresh')
|
||||
|
||||
const token = response.token
|
||||
const user = response.user
|
||||
|
||||
commit('auth_success', {
|
||||
token: token,
|
||||
user: user
|
||||
})
|
||||
|
||||
return token
|
||||
} catch (error) {
|
||||
commit('auth_error')
|
||||
console.error(error)
|
||||
}
|
||||
},
|
||||
async userInfo({commit}: {commit: any}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios.get_auth_data('v1/auth/info')
|
||||
.then((response: any) => {
|
||||
const user = response.user
|
||||
|
||||
commit('auth_success', {
|
||||
user: user
|
||||
})
|
||||
|
||||
resolve(user)
|
||||
}).catch(error => {
|
||||
reject(error)
|
||||
})
|
||||
})
|
||||
},
|
||||
updateUserInfo({commit}: {commit: any}, data: any) {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios.post_auth_data('v1/auth/info', data)
|
||||
.then((response: any) => {
|
||||
const user = response.user
|
||||
|
||||
commit('auth_success', {
|
||||
user: user
|
||||
})
|
||||
|
||||
resolve(user)
|
||||
}).catch(error => {
|
||||
reject(error)
|
||||
})
|
||||
})
|
||||
},
|
||||
},
|
||||
getters: {
|
||||
isLoggedIn: (state: any) => {
|
||||
return !!state._token
|
||||
},
|
||||
// authStatus: (state: any) => {
|
||||
// return state.status
|
||||
// },
|
||||
user: (state: any) => {
|
||||
return state.user
|
||||
}
|
||||
// token: (state: any) => {
|
||||
// return state._token
|
||||
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { App } from 'vue'
|
||||
import { Options } from './types'
|
||||
import auth from './auth.store'
|
||||
|
||||
export default {
|
||||
install: (app: App, options: Options) => {
|
||||
if (options && options.disabled == true) return
|
||||
|
||||
if (!app.config.globalProperties.$store)
|
||||
return console.warn('[w/auth] Could not initialize. App does not have vuex store added.')
|
||||
|
||||
app.config.globalProperties.$store.registerModule('auth', auth)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import store from '@/store'
|
||||
|
||||
export default async function(to, from, next) {
|
||||
if (to.matched.some((record) => record.meta.requiresAuth)) {
|
||||
if (!store.getters['auth/isLoggedIn']) {
|
||||
return next({ name: 'login' })
|
||||
}
|
||||
}
|
||||
|
||||
return next()
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import store from '@/store'
|
||||
import axios from 'axios'
|
||||
import createAuthRefreshInterceptor from 'axios-auth-refresh'
|
||||
|
||||
axios.interceptors.request.use(request => {
|
||||
request.headers['Authorization'] = `Bearer ${store.getters['user/token']}`
|
||||
return request
|
||||
})
|
||||
|
||||
const refreshAuthLogic = failedRequest => store.dispatch('user/refreshToken').then(token => {
|
||||
failedRequest.response.config.headers.Authorization = `Bearer ${token}`
|
||||
return Promise.resolve()
|
||||
})
|
||||
|
||||
createAuthRefreshInterceptor(axios, refreshAuthLogic)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export interface Options {
|
||||
disabled?: Boolean
|
||||
}
|
||||
Reference in new issue
Block a user