Skip to content

Commit e1b962f

Browse files
committed
Add manual routing
1 parent a58447b commit e1b962f

File tree

8 files changed

+52
-12
lines changed

8 files changed

+52
-12
lines changed

web/layouts/default.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default class DefaultLayout extends Vue {
3232
mounted() {
3333
this.startPoller()
3434
if (this.$route.name !== 'index') {
35+
this.$store.dispatch('setNextRoute', this.$route.path)
3536
this.$router.push({ name: 'index' })
3637
}
3738
}

web/middleware/auth.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { Context, Middleware } from '@nuxt/types'
2-
import { NotificationRequest } from '~/store'
32

4-
const authMiddleware: Middleware = async (context: Context) => {
3+
const authMiddleware: Middleware = (context: Context) => {
54
if (context.store.getters.getAuthUser === null) {
6-
const notification: NotificationRequest = {
7-
message: 'Login to continue',
8-
type: 'info',
5+
if (context.store.getters.getNextRoute) {
6+
context.redirect('/login', { to: context.route.path })
7+
context.store.dispatch('setNextRoute', null)
98
}
10-
await context.store.dispatch('addNotification', notification)
11-
12-
context.redirect('/login', { to: context.route.path })
139
}
1410
}
1511

web/middleware/guest.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { Context, Middleware } from '@nuxt/types'
22

33
const authMiddleware: Middleware = (context: Context) => {
44
if (context.store.getters.getAuthUser !== null) {
5+
if (context.store.getters.getNextRoute) {
6+
context.redirect(context.store.getters.getNextRoute)
7+
context.store.dispatch('setNextRoute', null)
8+
}
59
context.redirect('/threads')
610
}
711
}

web/middleware/user.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default async function (context: Context) {
88
}
99

1010
const setUser = (context: Context): Promise<User | null> => {
11+
console.log('running user')
1112
return new Promise((resolve, reject) => {
1213
const unsubscribe = (context.app.$fire.auth as Auth).onAuthStateChanged(
1314
async (user) => {

web/pages/settings/index.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ export default {
6767
},
6868
},
6969
mounted() {
70+
if (!this.$store.getters.getAuthUser) {
71+
this.$store.dispatch('setNextRoute', this.$route.path)
72+
this.$router.push({ name: 'index' })
73+
return
74+
}
7075
this.$store.dispatch('loadUser')
7176
},
7277
}

web/pages/threads/_id.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ export default Vue.extend({
215215
},
216216
217217
async mounted() {
218+
if (!this.$store.getters.getAuthUser) {
219+
await this.$store.dispatch('setNextRoute', this.$route.path)
220+
await this.$router.push({ name: 'index' })
221+
return
222+
}
223+
218224
await this.$store.dispatch('loadPhones')
219225
await this.$store.dispatch('loadThreads')
220226

web/pages/threads/index.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@
2828
export default {
2929
name: 'ThreadsIndex',
3030
middleware: ['auth'],
31-
mounted() {
32-
this.$store.dispatch('loadThreads')
33-
this.$store.dispatch('loadPhones')
31+
async mounted() {
32+
if (!this.$store.getters.getAuthUser) {
33+
await this.$store.dispatch('setNextRoute', this.$route.path)
34+
await this.$router.push({ name: 'index' })
35+
return
36+
}
37+
await this.$store.dispatch('loadThreads')
38+
await this.$store.dispatch('loadPhones')
3439
},
3540
}
3641
</script>

web/store/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type AuthUser = {
2828

2929
export type State = {
3030
owner: string | null
31+
nextRoute: string | null
3132
loadingThreads: boolean
3233
loadingMessages: boolean
3334
archivedThreads: boolean
@@ -46,6 +47,7 @@ export const state = (): State => ({
4647
threads: [],
4748
threadId: null,
4849
heartbeat: null,
50+
nextRoute: null,
4951
loadingThreads: true,
5052
archivedThreads: false,
5153
loadingMessages: true,
@@ -130,6 +132,10 @@ export const getters = {
130132
return state.loadingMessages
131133
},
132134

135+
getNextRoute(state: State): string | null {
136+
return state.nextRoute
137+
},
138+
133139
getThreadMessages(state: State): Array<Message> {
134140
return state.threadMessages
135141
},
@@ -218,6 +224,10 @@ export const mutations = {
218224
state.archivedThreads = false
219225
state.owner = null
220226
},
227+
228+
setNextRoute(state: State, payload: string | null) {
229+
state.nextRoute = payload
230+
},
221231
}
222232

223233
export type SendMessageRequest = {
@@ -250,6 +260,10 @@ export const actions = {
250260
if (context.getters.getPhones.length > 0 && !force) {
251261
return
252262
}
263+
if (!context.getters.authUser) {
264+
return
265+
}
266+
253267
const response = await axios.get('/v1/phones', { params: { limit: 100 } })
254268
context.commit('setPhones', response.data.data)
255269
},
@@ -309,6 +323,10 @@ export const actions = {
309323
context.commit('disableNotification')
310324
},
311325

326+
setNextRoute(context: ActionContext<State, State>, payload: string | null) {
327+
context.commit('setNextRoute', payload)
328+
},
329+
312330
async loadThreadMessages(
313331
context: ActionContext<State, State>,
314332
threadId: string | null
@@ -325,10 +343,14 @@ export const actions = {
325343

326344
async setAuthUser(
327345
context: ActionContext<State, State>,
328-
user: AuthUser | null
346+
user: AuthUser | null | undefined
329347
) {
330348
const userChanged = user?.id !== context.getters.getAuthUser?.id
331349

350+
if (user === undefined) {
351+
user = null
352+
}
353+
332354
await context.commit('setAuthUser', user)
333355

334356
if (userChanged && user !== null) {

0 commit comments

Comments
 (0)