Skip to content

Commit d78aa21

Browse files
committed
feat: implement redirect if page requires auth, todo: redirect on logout
1 parent 63680c1 commit d78aa21

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

src/composables/stores/auth.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@ import { cloneDeep } from 'lodash'
33
import { authApi } from '@/api'
44
import { PreferencesStore } from '@/composables/stores/prefs'
55
import PermissionUtils from '@/composables/utils/permissions'
6+
import localStorageCache from '@/composables/utils/localStorageCache'
67

78
const 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

920
export const AuthStore = Symbol(AUTH_KEY)
1021

22+
export const localStorageAuth = () => appCache.get(AUTH_KEY) || { data: emtpyUser }
23+
1124
export 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, {

src/router/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import NotFound from '@/views/layout/NotFound.vue'
99
import Login from '@/views/layout/Login.vue'
1010
import NProgress from 'nprogress'
1111
import { nextTick } from 'vue'
12+
import { localStorageAuth } from '@/composables/stores/auth'
1213

1314
const routes = [
1415
{
@@ -21,6 +22,7 @@ const routes = [
2122
path: '/login',
2223
name: 'Login',
2324
component: Login,
25+
props: true,
2426
meta: { requiresAuth: false, bodyClass: 'login' }
2527
},
2628
{
@@ -75,9 +77,13 @@ const router = createRouter({
7577
}
7678
})
7779

78-
router.beforeEach(() => {
80+
router.beforeEach((to) => {
7981
// Start progress bar
8082
NProgress.start()
83+
84+
if (to.meta.requiresAuth && !localStorageAuth().data.token) {
85+
router.push({ name: 'Login', params: { redirectTo: to.name || 'Boards' } })
86+
}
8187
})
8288

8389
router.afterEach(to => {

src/views/layout/Login.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,27 @@
3838
<script>
3939
import { reactive, inject, toRefs, nextTick } from 'vue'
4040
import { AuthStore } from '@/composables/stores/auth'
41+
import { useRouter } from 'vue-router'
4142
4243
export default {
4344
name: 'Login',
44-
setup() {
45+
props: ['redirectTo'],
46+
setup(props) {
4547
nextTick(() => v.focusInput.focus())
4648
/* Template Methods */
4749
const login = () => {
4850
$auth.login(v.user.username, v.user.password, v.user.rememberMe)
51+
.then(() => {
52+
$router.push( { name: props.redirectTo })
53+
})
4954
}
5055
5156
const signInWithGoogle = () => console.log('Sign in with Google!')
5257
5358
5459
/* Internal Data */
5560
const $auth = inject(AuthStore)
61+
const $router = useRouter()
5662
5763
/* Template Data */
5864
const v = reactive({

0 commit comments

Comments
 (0)