Skip to content

Commit

Permalink
feature: redirect to login if no access token
Browse files Browse the repository at this point in the history
  • Loading branch information
abdahmed22 committed Sep 8, 2024
1 parent 39d7b4b commit d823ec2
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 29 deletions.
8 changes: 4 additions & 4 deletions client/src/api/authService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { axios } from './axios'
import md5 from 'md5'
import { logInInfo, signUpInfo } from '../types/types'
import { UserProfile } from '../types/types'

export async function signUp (newUser:signUpInfo) {
export async function signUp (newUser: Partial<UserProfile>) {
try {
await axios.post('/auth/signup/', newUser)
} catch (error) {
Expand All @@ -20,13 +20,13 @@ export async function signUpInvitation (password:string) {
}
}

export async function logInUser (userInfo: logInInfo) {
export async function logInUser (userInfo: Partial<UserProfile>) {
try {
const response = await axios.post('/auth/login/', userInfo)
const token = response.data.access_token
const refreshtoken = response.data.refresh_token
localStorage.setItem('TEST_TRACKER_ACCESS_TOKEN', token)
localStorage.setItem('TTPHASH', md5(userInfo.password))
localStorage.setItem('TTPHASH', md5(userInfo.password!))
localStorage.setItem('TEST_TRACKER_REFRESH_TOKEN', refreshtoken)
} catch (error) {
localStorage.removeItem('TEST_TRACKER_ACCESS_TOKEN')
Expand Down
2 changes: 1 addition & 1 deletion client/src/api/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export const axios = axiosClient.create({
timeout: 1000,
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzI1NzkzNDY0LCJpYXQiOjE3MjU3ODQxNjQsImp0aSI6ImI0Nzk5NTE3NmJlODQ5NDRhODA2MmI5NTUxYWI4OWNmIiwidXNlcl9pZCI6NSwiZW1haWwiOiJib3VkaWUyMDAzQGdtYWlsLmNvbSJ9.LG02kVuoHWFycqcVtATITE6qe7LYvaGHUgwjWm8OqYo',
Authorization: 'Bearer ' + localStorage.getItem('TEST_TRACKER_ACCESS_TOKEN'),
},
})
2 changes: 1 addition & 1 deletion client/src/api/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export async function putSettings (settings :Partial<UserProfile>) {
}

export async function putPassword (passwords :Partial<Password>) {
localStorage.setItem('TTPHASH', md5(passwords.new_password))
// localStorage.setItem('TTPHASH', md5(passwords.new_password!)) // only apply when the request succeeds
return axios.put('/auth/change-password/', passwords)
}
2 changes: 1 addition & 1 deletion client/src/components/settings/ProfileInformationForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
.catch((err: any) => {
let description = 'Can not update profile settings'
if (err.response) {
description = err.response.data.detail
description = err.response.data.message
}
notifier.notify({
title: 'Fail',
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/settings/SecurityForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
.catch((err: any) => {
let description = 'Can not update password'
if (err.response) {
description = err.response.data.detail
description = err.response.data.message
}
notifier.notify({
title: 'Fail',
Expand Down
8 changes: 4 additions & 4 deletions client/src/pages/authorization/LoginView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@
</template>

<script lang="ts">
import api from '@/api/authService'
import { logInUser } from '@/api/authService'
import { useRouter } from 'vue-router'
import { useNotifier } from 'vue3-notifier'
import { emailRules, passwordRules } from '@/utilities/validators'
import type { logInInfo } from '@/types/types.ts'
import { UserProfile } from '../../types/types'
export default {
name: 'LoginView',
Expand All @@ -126,7 +126,7 @@
const notifier = useNotifier('top right')
const userInfo = ref<logInInfo>({
const userInfo = ref<Partial<UserProfile>>({
email: '',
password: '',
})
Expand All @@ -136,7 +136,7 @@
async function SubmitLogIn () {
try {
localStorage.removeItem('TEST_TRACKER_REFRESH_TOKEN')
await api.logInUser(userInfo.value)
await logInUser(userInfo.value)
if (localStorage.getItem('TEST_TRACKER_REFRESH_TOKEN') != null) {
router.push(`/`)
}
Expand Down
8 changes: 4 additions & 4 deletions client/src/pages/authorization/SignupView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@
</template>

<script lang="ts">
import api from '@/api/authService'
import { signUp } from '@/api/authService'
import { useNotifier } from 'vue3-notifier'
import { useRouter } from 'vue-router'
import { emailRules, nameRules, passwordRules } from '@/utilities/validators'
import LoginHintComponent from '@/components/LoginHintComponent.vue'
import { signUpInfo } from '@/types/types'
import { UserProfile } from '../../types/types'
export default {
name: 'SignupView',
Expand All @@ -119,7 +119,7 @@
const router = useRouter()
const loading = ref(false)
const newUser = ref<signUpInfo>({
const newUser = ref<Partial<UserProfile>>({
first_name: '',
last_name: '',
email: '',
Expand All @@ -131,7 +131,7 @@
async function RegisterNewUser () {
loading.value = true
try {
await api.signUp(newUser.value)
await signUp(newUser.value)
loading.value = false
notifier.notify({
title: 'Success',
Expand Down
12 changes: 10 additions & 2 deletions client/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ const router = createRouter({
],
})

router.beforeEach((to, from) => {
router.beforeEach((to, from, next) => {
const routeStore = useCurrentRouteStore()
const authRoutes = ['signup', 'signup-invitation', 'login']
const routeName = to.name

routeStore.changeCurrentRoute(to.name)
if (!localStorage.getItem('TEST_TRACKER_ACCESS_TOKEN') && !authRoutes.includes(routeName)) {
routeStore.changeCurrentRoute('login')
next({ name: 'login' })
} else {
routeStore.changeCurrentRoute(routeName)
next()
}
})

// Workaround for https://github.com/vitejs/vite/issues/11804
Expand Down
11 changes: 0 additions & 11 deletions client/src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
export interface logInInfo {
email: string;
password: string;
}

export interface signUpInfo{
first_name: string;
last_name: string;
email: string;
password: string;
}
export type UserProfile = {
email: string,
first_name: string,
Expand Down

0 comments on commit d823ec2

Please sign in to comment.