Skip to content

Commit

Permalink
Storage auth token vars in sessionStorage in online survey app
Browse files Browse the repository at this point in the history
  • Loading branch information
esurface committed Aug 1, 2024
1 parent 75cda62 commit 30b61d3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion online-survey-app/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class AppComponent implements OnInit{
}

async sessionTimeoutCheck() {
const token = localStorage.getItem('token');
const token = sessionStorage.getItem('token');
const claims = JSON.parse(atob(token.split('.')[1]));
const expiryTimeInMs = claims['exp'] * 1000;
const minutesBeforeExpiry = expiryTimeInMs - (15 * 60 * 1000); // warn 15 minutes before expiry of token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class LoginComponent implements OnInit {

if (window.location.origin.startsWith('http://localhost')) {
// If we are running on localhost, we want to use the local server for authentication
localStorage.setItem(this.user.username, this.user.password);
sessionStorage.setItem(this.user.username, this.user.password);
this.router.navigate([this.returnUrl]);
} else if (await this.authenticationService.login(this.user.username, this.user.password)) {
this.router.navigate([this.returnUrl]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export class AuthenticationService {
}
} catch (error) {
console.error(error);
localStorage.removeItem('token');
localStorage.removeItem('user_id');
localStorage.removeItem('permissions');
sessionStorage.removeItem('token');
sessionStorage.removeItem('user_id');
sessionStorage.removeItem('permissions');
return false;
}
}
Expand All @@ -50,26 +50,26 @@ export class AuthenticationService {
}
} catch (error) {
console.error(error);
localStorage.removeItem('token');
localStorage.removeItem('user_id');
localStorage.removeItem('password');
localStorage.removeItem('permissions');
sessionStorage.removeItem('token');
sessionStorage.removeItem('user_id');
sessionStorage.removeItem('password');
sessionStorage.removeItem('permissions');
return false;
}
}

async isLoggedIn():Promise<boolean> {
this._currentUserLoggedIn = false;
this._currentUserLoggedIn = !!localStorage.getItem('user_id');
this._currentUserLoggedIn = !!sessionStorage.getItem('user_id');
this.currentUserLoggedIn$.next(this._currentUserLoggedIn);
return this._currentUserLoggedIn;
}

async logout() {
localStorage.removeItem('token');
localStorage.removeItem('user_id');
localStorage.removeItem('password');
localStorage.removeItem('permissions');
sessionStorage.removeItem('token');
sessionStorage.removeItem('user_id');
sessionStorage.removeItem('password');
sessionStorage.removeItem('permissions');
document.cookie = "Authorization=;max-age=-1";
this._currentUserLoggedIn = false;
this.currentUserLoggedIn$.next(this._currentUserLoggedIn);
Expand All @@ -78,7 +78,7 @@ export class AuthenticationService {
async extendUserSession() {
const appConfig = await this.appConfigService.getAppConfig();
const groupId = appConfig['groupId'];
const accessCode = localStorage.getItem('user_id');
const accessCode = sessionStorage.getItem('user_id');

try {
const data = await this.http.post(`/onlineSurvey/login/${groupId}/${accessCode}`, {groupId, accessCode}, {observe: 'response'}).toPromise();
Expand All @@ -98,9 +98,9 @@ export class AuthenticationService {
async setTokens(token) {
const jwtData = jwtDecode(token);
document.cookie = "Authorization=;max-age=-1";
localStorage.setItem('token', token);
localStorage.setItem('user_id', jwtData['username']);
localStorage.setItem('permissions', JSON.stringify(jwtData['permissions']));
sessionStorage.setItem('token', token);
sessionStorage.setItem('user_id', jwtData['username']);
sessionStorage.setItem('permissions', JSON.stringify(jwtData['permissions']));
document.cookie = `Authorization=${token}`;
}

Expand Down
4 changes: 2 additions & 2 deletions online-survey-app/src/app/core/auth/_services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class UserService {
}

async getCurrentUser() {
return await localStorage.getItem('user_id');
return await sessionStorage.getItem('user_id');
}
private showError(error: any) {
console.log(error);
Expand All @@ -58,7 +58,7 @@ export class UserService {

async getMyUser() {
try {
if (localStorage.getItem('user_id') === 'user1') {
if (sessionStorage.getItem('user_id') === 'user1') {
return {
email: '[email protected]',
firstName: 'user1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class UserDatabase {
}

async get(id) {
const token = localStorage.getItem('token');
const token = sessionStorage.getItem('token');
return (<any>await axios.get(`/group-responses/read/${this.groupId}/${id}`, { headers: { authorization: token }})).data
}

Expand All @@ -33,7 +33,7 @@ export class UserDatabase {
}

async post(doc) {
const token = localStorage.getItem('token');
const token = sessionStorage.getItem('token');
if (this.attachHistoryToDocs === undefined) {
const appConfig = (<any>await axios.get('./assets/app-config.json', { headers: { authorization: token }})).data
this.attachHistoryToDocs = appConfig['attachHistoryToDocs']
Expand Down Expand Up @@ -70,7 +70,7 @@ export class UserDatabase {

async remove(doc) {
// This is not implemented...
const token = localStorage.getItem('token');
const token = sessionStorage.getItem('token');
return await axios.delete(`/api/${this.groupId}`, doc)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class TangyFormService {
}

initialize(groupId) {
this.userId = localStorage.getItem('user_id') || 'Survey'
this.userId = sessionStorage.getItem('user_id') || 'Survey'
this.db = new UserDatabase(this.userId, groupId)
}

Expand Down

0 comments on commit 30b61d3

Please sign in to comment.