Skip to content

Commit

Permalink
Fix online survey extend session call
Browse files Browse the repository at this point in the history
  • Loading branch information
esurface committed Jul 1, 2024
1 parent cc68399 commit ab92d86
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AuthenticationService } from '../../_services/authentication.service';
import { _TRANSLATE } from 'src/app/shared/_services/translation-marker';
import { AppConfigService } from 'src/app/shared/_services/app-config.service';


@Component({
selector: 'survey-login',
Expand All @@ -20,7 +18,6 @@ export class SurveyLoginComponent implements OnInit {

constructor(
private authenticationService: AuthenticationService,
private appConfigService: AppConfigService,
private route: ActivatedRoute,
private router: Router,
) { }
Expand Down Expand Up @@ -55,11 +52,7 @@ export class SurveyLoginComponent implements OnInit {

async loginUser() {
try {

const appConfig = await this.appConfigService.getAppConfig();
const groupId = appConfig['groupId'];

if (await this.authenticationService.surveyLogin(groupId, this.user.accessCode)) {
if (await this.authenticationService.surveyLogin(this.user.accessCode)) {
this.router.navigate([this.returnUrl]);
} else {
this.errorMessage = _TRANSLATE('Login Unsuccessful');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { UserService } from './user.service';
import { Subject } from 'rxjs';
import { jwtDecode } from 'jwt-decode';
import { _TRANSLATE } from 'src/app/shared/_services/translation-marker';
import { TangyErrorHandler } from 'src/app/shared/_services/tangy-error-handler.service';
import { AppConfigService } from 'src/app/shared/_services/app-config.service';

@Injectable()
export class AuthenticationService {
public currentUserLoggedIn$: any;
private _currentUserLoggedIn: boolean;
constructor(private userService: UserService, private http: HttpClient, private errorHandler: TangyErrorHandler) {
constructor(
private http: HttpClient,
private appConfigService: AppConfigService
) {
this.currentUserLoggedIn$ = new Subject();
}

Expand All @@ -33,7 +35,10 @@ export class AuthenticationService {
}
}

async surveyLogin(groupId: string, accessCode: string) {
async surveyLogin(accessCode: string) {
const appConfig = await this.appConfigService.getAppConfig();
const groupId = appConfig['groupId'];

try {
const data = await this.http.post(`/onlineSurvey/login/${groupId}/${accessCode}`, {groupId, accessCode}, {observe: 'response'}).toPromise();
if (data.status === 200) {
Expand Down Expand Up @@ -71,9 +76,12 @@ export class AuthenticationService {
}

async extendUserSession() {
const username = localStorage.getItem('user_id');
const appConfig = await this.appConfigService.getAppConfig();
const groupId = appConfig['groupId'];
const accessCode = localStorage.getItem('user_id');

try {
const data = await this.http.post('/extendSession', {username}, {observe: 'response'}).toPromise();
const data = await this.http.post(`/onlineSurvey/login/${groupId}/${accessCode}`, {groupId, accessCode}, {observe: 'response'}).toPromise();
if (data.status === 200) {
const token = data.body['data']['token'];
await this.setTokens(token);
Expand Down
8 changes: 4 additions & 4 deletions server/src/online-survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const login = async (req, res) => {

const { groupId, accessCode } = req.params;

console.log('login', groupId, accessCode);

const groupDb = new DB(groupId)
let options = { key: accessCode, include_docs: true }
if (req.params.limit) {
Expand All @@ -24,14 +22,16 @@ const login = async (req, res) => {
const userProfileDoc = docs.find(doc => doc.form.id === 'user-profile');

if (userProfileDoc) {
debugger;
let permissions = {groupPermissions: [], sitewidePermissions: []};
const token = createLoginJWT({ "username": accessCode, permissions });
return res.status(200).send({ data: { token } });
} else {
console.error(error);
return res.status(401).send({ data: 'Invalid Credentials' });
}
} catch (error) {
console.error(error);
return res.status(401).send({ data: 'Invalid Credentials' });
return res.status(500).send({ data: 'An error occurred attempting to login' });
}
}

Expand Down

0 comments on commit ab92d86

Please sign in to comment.