Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Add manual routing

Acho Arnolde1b962fd6ac6
diff --git a/web/layouts/default.vue b/web/layouts/default.vue
index 3a03f4cc..83befc0e 100644
--- a/web/layouts/default.vue
+++ b/web/layouts/default.vue
@@ -32,6 +32,7 @@ export default class DefaultLayout extends Vue {
mounted() {
this.startPoller()
if (this.$route.name !== 'index') {
+ this.$store.dispatch('setNextRoute', this.$route.path)
this.$router.push({ name: 'index' })
}
}
diff --git a/web/middleware/auth.ts b/web/middleware/auth.ts
index 4159f0a9..57ce6895 100644
--- a/web/middleware/auth.ts
+++ b/web/middleware/auth.ts
@@ -1,15 +1,11 @@
import { Context, Middleware } from '@nuxt/types'
-import { NotificationRequest } from '~/store'
-const authMiddleware: Middleware = async (context: Context) => {
+const authMiddleware: Middleware = (context: Context) => {
if (context.store.getters.getAuthUser === null) {
- const notification: NotificationRequest = {
- message: 'Login to continue',
- type: 'info',
+ if (context.store.getters.getNextRoute) {
+ context.redirect('/login', { to: context.route.path })
+ context.store.dispatch('setNextRoute', null)
}
- await context.store.dispatch('addNotification', notification)
-
- context.redirect('/login', { to: context.route.path })
}
}
diff --git a/web/middleware/guest.ts b/web/middleware/guest.ts
index 257435a4..f9bc2760 100644
--- a/web/middleware/guest.ts
+++ b/web/middleware/guest.ts
@@ -2,6 +2,10 @@ import { Context, Middleware } from '@nuxt/types'
const authMiddleware: Middleware = (context: Context) => {
if (context.store.getters.getAuthUser !== null) {
+ if (context.store.getters.getNextRoute) {
+ context.redirect(context.store.getters.getNextRoute)
+ context.store.dispatch('setNextRoute', null)
+ }
context.redirect('/threads')
}
}
diff --git a/web/middleware/user.ts b/web/middleware/user.ts
index f7316d9c..48b10cf9 100644
--- a/web/middleware/user.ts
+++ b/web/middleware/user.ts
@@ -8,6 +8,7 @@ export default async function (context: Context) {
}
const setUser = (context: Context): Promise<User | null> => {
+ console.log('running user')
return new Promise((resolve, reject) => {
const unsubscribe = (context.app.$fire.auth as Auth).onAuthStateChanged(
async (user) => {
diff --git a/web/pages/settings/index.vue b/web/pages/settings/index.vue
index f167651e..3bfe5534 100644
--- a/web/pages/settings/index.vue
+++ b/web/pages/settings/index.vue
@@ -67,6 +67,11 @@ export default {
},
},
mounted() {
+ if (!this.$store.getters.getAuthUser) {
+ this.$store.dispatch('setNextRoute', this.$route.path)
+ this.$router.push({ name: 'index' })
+ return
+ }
this.$store.dispatch('loadUser')
},
}
diff --git a/web/pages/threads/_id.vue b/web/pages/threads/_id.vue
index 5ae3d9c0..d49e6644 100644
--- a/web/pages/threads/_id.vue
+++ b/web/pages/threads/_id.vue
@@ -215,6 +215,12 @@ export default Vue.extend({
},
async mounted() {
+ if (!this.$store.getters.getAuthUser) {
+ await this.$store.dispatch('setNextRoute', this.$route.path)
+ await this.$router.push({ name: 'index' })
+ return
+ }
+
await this.$store.dispatch('loadPhones')
await this.$store.dispatch('loadThreads')
diff --git a/web/pages/threads/index.vue b/web/pages/threads/index.vue
index 2287b841..365b097b 100644
--- a/web/pages/threads/index.vue
+++ b/web/pages/threads/index.vue
@@ -28,9 +28,14 @@
export default {
name: 'ThreadsIndex',
middleware: ['auth'],
- mounted() {
- this.$store.dispatch('loadThreads')
- this.$store.dispatch('loadPhones')
+ async mounted() {
+ if (!this.$store.getters.getAuthUser) {
+ await this.$store.dispatch('setNextRoute', this.$route.path)
+ await this.$router.push({ name: 'index' })
+ return
+ }
+ await this.$store.dispatch('loadThreads')
+ await this.$store.dispatch('loadPhones')
},
}
</script>
diff --git a/web/store/index.ts b/web/store/index.ts
index c77b6bec..b2fb5db4 100644
--- a/web/store/index.ts
+++ b/web/store/index.ts
@@ -28,6 +28,7 @@ export type AuthUser = {
export type State = {
owner: string | null
+ nextRoute: string | null
loadingThreads: boolean
loadingMessages: boolean
archivedThreads: boolean
@@ -46,6 +47,7 @@ export const state = (): State => ({
threads: [],
threadId: null,
heartbeat: null,
+ nextRoute: null,
loadingThreads: true,
archivedThreads: false,
loadingMessages: true,
@@ -130,6 +132,10 @@ export const getters = {
return state.loadingMessages
},
+ getNextRoute(state: State): string | null {
+ return state.nextRoute
+ },
+
getThreadMessages(state: State): Array<Message> {
return state.threadMessages
},
@@ -218,6 +224,10 @@ export const mutations = {
state.archivedThreads = false
state.owner = null
},
+
+ setNextRoute(state: State, payload: string | null) {
+ state.nextRoute = payload
+ },
}
export type SendMessageRequest = {
@@ -250,6 +260,10 @@ export const actions = {
if (context.getters.getPhones.length > 0 && !force) {
return
}
+ if (!context.getters.authUser) {
+ return
+ }
+
const response = await axios.get('/v1/phones', { params: { limit: 100 } })
context.commit('setPhones', response.data.data)
},
@@ -309,6 +323,10 @@ export const actions = {
context.commit('disableNotification')
},
+ setNextRoute(context: ActionContext<State, State>, payload: string | null) {
+ context.commit('setNextRoute', payload)
+ },
+
async loadThreadMessages(
context: ActionContext<State, State>,
threadId: string | null
@@ -325,10 +343,14 @@ export const actions = {
async setAuthUser(
context: ActionContext<State, State>,
- user: AuthUser | null
+ user: AuthUser | null | undefined
) {
const userChanged = user?.id !== context.getters.getAuthUser?.id
+ if (user === undefined) {
+ user = null
+ }
+
await context.commit('setAuthUser', user)
if (userChanged && user !== null) {