@@ -3,11 +3,24 @@ import { cloneDeep } from 'lodash'
33import { authApi } from '@/api'
44import { PreferencesStore } from '@/composables/stores/prefs'
55import PermissionUtils from '@/composables/utils/permissions'
6+ import localStorageCache from '@/composables/utils/localStorageCache'
67
78const AUTH_KEY = 'auth'
9+ const appCache = localStorageCache ( 0 , 'app' )
10+ const emtpyUser = {
11+ avatar : '' ,
12+ id : null ,
13+ moderating : [ ] ,
14+ permissions : { } ,
15+ roles : [ ] ,
16+ token : null ,
17+ username : ''
18+ }
819
920export const AuthStore = Symbol ( AUTH_KEY )
1021
22+ export const localStorageAuth = ( ) => appCache . get ( AUTH_KEY ) || { data : emtpyUser }
23+
1124export default {
1225 setup ( ) {
1326 /* Internal Data */
@@ -16,32 +29,21 @@ export default {
1629 const $prefs = inject ( PreferencesStore )
1730
1831 const cachedUser = $appCache . get ( AUTH_KEY )
19- const emtpyUser = {
20- avatar : '' ,
21- id : null ,
22- moderating : [ ] ,
23- permissions : { } ,
24- roles : [ ] ,
25- token : null ,
26- username : ''
27- }
2832
2933 /* Provided Data */
3034 const user = reactive ( cachedUser ? cachedUser . data : cloneDeep ( emtpyUser ) )
3135
3236 /* Provided Methods */
33- const login = ( username , password , rememberMe ) => {
34- authApi . login ( { username, password, rememberMe } )
37+ const login = ( username , password , rememberMe ) => authApi . login ( { username, password, rememberMe } )
3538 . then ( dbUser => {
3639 $appCache . set ( AUTH_KEY , dbUser )
3740 Object . assign ( user , dbUser )
3841 $prefs . fetch ( )
3942 $alertStore . success ( `Welcome ${ user . username } , you have successfully logged in!` )
4043 } ) . catch ( ( ) => { } )
41- }
4244
43- const logout = ( ) => {
44- authApi . logout ( )
45+
46+ const logout = ( ) => authApi . logout ( )
4547 . then ( ( ) => {
4648 delete user . token // clear token to invalidate session immediately
4749 $appCache . delete ( AUTH_KEY )
@@ -51,10 +53,7 @@ export default {
5153 setTimeout ( ( ) => { Object . assign ( user , cloneDeep ( emtpyUser ) ) } , 500 )
5254 } ) . catch ( ( ) => { } )
5355
54- }
55-
56- const register = ( email , username , password ) => {
57- authApi . register ( { email, username, password } )
56+ const register = ( email , username , password ) => authApi . register ( { email, username, password } )
5857 . then ( dbUser => {
5958 // Set user session if account is already confirmed (log the user in)
6059 if ( ! dbUser . confirm_token ) {
@@ -66,7 +65,6 @@ export default {
6665 // TODO(akinsey): implement flow for when email confirmation is enabled
6766 // else {}
6867 } ) . catch ( ( ) => { } )
69- }
7068
7169 /* Provide Store Data */
7270 return provide ( AuthStore , {
0 commit comments