Skip to content

Commit

Permalink
fix(#494): Linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tholulomo committed Jun 22, 2024
1 parent 573b12c commit 61df2c2
Showing 1 changed file with 104 additions and 80 deletions.
184 changes: 104 additions & 80 deletions app/src/store/modules/auth/actions.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import router from '@/router'
import router from '@/router';

let timer
let timer;
export default {
async login (context, payload) {
async login(context, payload) {
return context.dispatch('auth', {
...payload,
mode: 'login'
})
});
},

async signup (context, payload) {
async signup(context, payload) {
return context.dispatch('auth', {
...payload,
mode: 'signup'
})
});
},

async auth (context, payload) {
const mode = payload.mode
let url = 'https://server.test'
async auth(context, payload) {
const mode = payload.mode;
let url = 'https://server.test';

if (mode === 'signup') {
url = 'https://server.test'
url = 'https://server.test';
}

const response = await fetch(url, {
Expand All @@ -31,92 +31,112 @@ export default {
password: payload.password,
returnSecureToken: true
})
})
});

const responseData = await response.json()
const responseData = await response.json();

if (!response.ok) {
const error = new Error(
responseData.message || 'Failed to authenticate. Check your login data.'
)
throw error
);
throw error;
}

return context.dispatch('authProcessor', responseData)
return context.dispatch('authProcessor', responseData);
},

async authProcessor (context, payload) {
const res = payload ? JSON.parse(payload) : {}

// Reroute to home page
// TODO (xxx): We should re-route to the page where user left off
router.push('/nm')
context.commit('setSnackbar', {
message: 'Authenticating...',
duration: 3000
}, { root: true })

const token = res.token ?? null
const userId = res.userId ?? null
const displayName = res.displayName ?? null
const surName = res.surName ?? null
const givenName = res.givenName ?? null
const isAdmin = res.isAdmin ?? false
const expiresIn = 9000 * 60 * 60
const expirationDate = new Date().getTime() + expiresIn
async authProcessor(context, payload) {
const res = payload ? JSON.parse(payload) : {};
context.commit(
'setSnackbar',
{
message: 'Authenticating...',
duration: 3000
},
{ root: true }
);

const token = res.token ?? null;
const userId = res.userId ?? null;
const displayName = res.displayName ?? null;
const surName = res.surName ?? null;
const givenName = res.givenName ?? null;
// const isAdmin = res.isAdmin ?? false;
const isAdmin = false;
const expiresIn = 9000 * 60 * 60;
const expirationDate = new Date().getTime() + expiresIn;

if (token && userId && displayName) {
localStorage.setItem('token', token)
localStorage.setItem('userId', userId)
localStorage.setItem('displayName', displayName)
localStorage.setItem('surName', surName)
localStorage.setItem('givenName', givenName)
localStorage.setItem('isAdmin', isAdmin)
localStorage.setItem('tokenExpiration', expirationDate)
localStorage.setItem('token', token);
localStorage.setItem('userId', userId);
localStorage.setItem('displayName', displayName);
localStorage.setItem('surName', surName);
localStorage.setItem('givenName', givenName);
localStorage.setItem('isAdmin', isAdmin);
localStorage.setItem('tokenExpiration', expirationDate);

timer = setTimeout(function () {
context.dispatch('autoLogout')
}, expiresIn)
context.dispatch('autoLogout');
}, expiresIn);
}

context.commit('setUser', { token, userId, displayName, isAdmin, surName, givenName })
context.commit('setUser', {
token,
userId,
displayName,
isAdmin,
surName: context.getters.lastPageVisited,
givenName
});
// Reroute to home page
// TODO (xxx): We should re-route to the page where user left off
// if (this.router.path !== context.getters['lastPageVisited']) {
// }
return router.push(context.getters.lastPageVisited);
},

tryLogin (context) {
const token = localStorage.getItem('token')
const userId = localStorage.getItem('userId')
const displayName = localStorage.getItem('displayName')
const surName = localStorage.getItem('surName')
const givenName = localStorage.getItem('givenName')
const isAdmin = localStorage.getItem('isAdmin')
const tokenExpiration = localStorage.getItem('tokenExpiration')
tryLogin(context) {
const token = localStorage.getItem('token');
const userId = localStorage.getItem('userId');
const displayName = localStorage.getItem('displayName');
const surName = localStorage.getItem('surName');
const givenName = localStorage.getItem('givenName');
const isAdmin = localStorage.getItem('isAdmin');
const tokenExpiration = localStorage.getItem('tokenExpiration');

const expiresIn = +tokenExpiration - new Date().getTime()
const expiresIn = +tokenExpiration - new Date().getTime();

if (expiresIn < 0) {
context.dispatch('autoLogout')
return context.dispatch('notifyUser')
context.dispatch('autoLogout');
return context.dispatch('notifyUser');
}

timer = setTimeout(function () {
context.dispatch('autoLogout')
}, expiresIn)
context.dispatch('autoLogout');
}, expiresIn);

if (token && userId && displayName) {
context.commit('setUser', { token, userId, displayName, isAdmin, surName, givenName })
context.commit('setUser', {
token,
userId,
displayName,
isAdmin,
surName,
givenName
});
}
},

logout (context) {
localStorage.removeItem('token')
localStorage.removeItem('userId')
localStorage.removeItem('displayName')
localStorage.removeItem('surName')
localStorage.removeItem('givenName')
localStorage.removeItem('isAdmin')
localStorage.removeItem('tokenExpiration')
logout(context) {
localStorage.removeItem('token');
localStorage.removeItem('userId');
localStorage.removeItem('displayName');
localStorage.removeItem('surName');
localStorage.removeItem('givenName');
localStorage.removeItem('isAdmin');
localStorage.removeItem('tokenExpiration');

clearTimeout(timer)
clearTimeout(timer);

context.commit('setUser', {
token: null,
Expand All @@ -125,24 +145,28 @@ export default {
isAdmin: false,
surName: null,
givenName: null
})
});

const meta = router.currentRoute?.meta
const meta = router.currentRoute?.meta;
if (meta?.requiresAuth) {
router.push('/nm')
context.dispatch('notifyUser')
router.push('/nm');
context.dispatch('notifyUser');
}
},

autoLogout (context) {
context.dispatch('logout')
context.commit('setAutoLogout')
autoLogout(context) {
context.dispatch('logout');
context.commit('setAutoLogout');
},

notifyUser (context) {
context.commit('setSnackbar', {
message: 'This page requires login to access',
duration: 5000
}, { root: true })
notifyUser(context) {
context.commit(
'setSnackbar',
{
message: 'This page requires login to access',
duration: 5000
},
{ root: true }
);
}
}
};

0 comments on commit 61df2c2

Please sign in to comment.