Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Update reactivate and claim functionality to accept ORCID iD #2197

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 134 additions & 105 deletions src/app/core/sign-in/sign-in.service.ts
Original file line number Diff line number Diff line change
@@ -1,105 +1,134 @@
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { catchError, map, retry, switchMap, first } from 'rxjs/operators'

import { environment } from '../../../environments/environment'
import { getOrcidNumber } from '../../constants'
import { Reactivation } from '../../types/reactivation.endpoint'
import { SignIn } from '../../types/sign-in.endpoint'
import { SignInLocal, TypeSignIn } from '../../types/sign-in.local'
import { CustomEncoder } from '../custom-encoder/custom.encoder'
import { ErrorHandlerService } from '../error-handler/error-handler.service'
import { UserService } from '../user/user.service'
import { ERROR_REPORT } from 'src/app/errors'
import { Title } from '@angular/platform-browser'

@Injectable({
providedIn: 'root',
})
export class SignInService {
constructor(
private _http: HttpClient,
private _titleService: Title,
private _errorHandler: ErrorHandlerService,
private _userService: UserService
) {}
/**
* @param SignInLocal sign in information
* @param updateUserSession default true, set to true if after successfully signing Orcid Angular will still be open
* @param forceSessionUpdate default false, set to true if the user session should be updated even when the user status does not change
*/
signIn(
signInLocal: SignInLocal,
updateUserSession = true,
forceSessionUpdate = false
) {
let loginUrl = 'signin/auth.json'

if (signInLocal.type && signInLocal.type === TypeSignIn.institutional) {
loginUrl = 'shibboleth/signin/auth.json'
}

if (signInLocal.type && signInLocal.type === TypeSignIn.social) {
loginUrl = 'social/signin/auth.json'
}

let body = new HttpParams({ encoder: new CustomEncoder() })
.set('userId', getOrcidNumber(signInLocal.data.username))
.set('password', signInLocal.data.password)
if (signInLocal.data.verificationCode) {
body = body.set('verificationCode', signInLocal.data.verificationCode)
}
if (signInLocal.data.recoveryCode) {
body = body.set('recoveryCode', signInLocal.data.recoveryCode)
}
body = body.set('oauthRequest', signInLocal.isOauth ? 'true' : 'false')
return this._http
.post<SignIn>(environment.API_WEB + loginUrl, body, {
withCredentials: true,
})
.pipe(
retry(3),
catchError((error) => this._errorHandler.handleError(error)),
switchMap((response) => {
// call refreshUserSession with force session update to handle register actions from sessions with a logged in user
return this._userService
.refreshUserSession(forceSessionUpdate, true)
.pipe(
first(),
map(() => response)
)
})
)
}

reactivation(email: string) {
let body = new HttpParams({ encoder: new CustomEncoder() })
body = body.set('email', email)
return this._http
.post<Reactivation>(environment.API_WEB + `sendReactivation.json`, body, {
withCredentials: true,
})
.pipe(
retry(3),
catchError((error) =>
this._errorHandler.handleError(
error,
ERROR_REPORT.REGISTER_REACTIVATED_EMAIL
)
)
)
}

singOut() {
this._titleService.setTitle('ORCID')
return this._http
.get<SignIn>(environment.API_WEB + 'userStatus.json?logUserOut=true', {
withCredentials: true,
})
.pipe(
retry(3),
catchError((error) => this._errorHandler.handleError(error)),
switchMap(() => this._userService.refreshUserSession())
)
}
}
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { catchError, map, retry, switchMap, first } from 'rxjs/operators'

import { environment } from '../../../environments/environment'
import { getOrcidNumber, isValidOrcidFormat } from '../../constants'
import { Claim } from '../../types/claim.endpoint'
import { Reactivation } from '../../types/reactivation.endpoint'
import { SignIn } from '../../types/sign-in.endpoint'
import { SignInLocal, TypeSignIn } from '../../types/sign-in.local'
import { CustomEncoder } from '../custom-encoder/custom.encoder'
import { ErrorHandlerService } from '../error-handler/error-handler.service'
import { UserService } from '../user/user.service'
import { ERROR_REPORT } from 'src/app/errors'
import { Title } from '@angular/platform-browser'

@Injectable({
providedIn: 'root',
})
export class SignInService {
constructor(
private _http: HttpClient,
private _titleService: Title,
private _errorHandler: ErrorHandlerService,
private _userService: UserService
) {}
/**
* @param SignInLocal sign in information
* @param updateUserSession default true, set to true if after successfully signing Orcid Angular will still be open
* @param forceSessionUpdate default false, set to true if the user session should be updated even when the user status does not change
*/
signIn(
signInLocal: SignInLocal,
updateUserSession = true,
forceSessionUpdate = false
) {
let loginUrl = 'signin/auth.json'

if (signInLocal.type && signInLocal.type === TypeSignIn.institutional) {
loginUrl = 'shibboleth/signin/auth.json'
}

if (signInLocal.type && signInLocal.type === TypeSignIn.social) {
loginUrl = 'social/signin/auth.json'
}

let body = new HttpParams({ encoder: new CustomEncoder() })
.set('userId', getOrcidNumber(signInLocal.data.username))
.set('password', signInLocal.data.password)
if (signInLocal.data.verificationCode) {
body = body.set('verificationCode', signInLocal.data.verificationCode)
}
if (signInLocal.data.recoveryCode) {
body = body.set('recoveryCode', signInLocal.data.recoveryCode)
}
body = body.set('oauthRequest', signInLocal.isOauth ? 'true' : 'false')
return this._http
.post<SignIn>(environment.API_WEB + loginUrl, body, {
withCredentials: true,
})
.pipe(
retry(3),
catchError((error) => this._errorHandler.handleError(error)),
switchMap((response) => {
// call refreshUserSession with force session update to handle register actions from sessions with a logged in user
return this._userService
.refreshUserSession(forceSessionUpdate, true)
.pipe(
first(),
map(() => response)
)
})
)
}

reactivation(username: string) {
let body = new HttpParams({ encoder: new CustomEncoder() })
body = body.set(isValidOrcidFormat(username) ? 'orcid' : 'email', username)
return this._http
.post<Reactivation>(environment.API_WEB + `sendReactivation.json`, body, {
withCredentials: true,
})
.pipe(
retry(3),
catchError((error) =>
this._errorHandler.handleError(
error,
ERROR_REPORT.REGISTER_REACTIVATED_EMAIL
)
)
)
}

resendClaim(username: string) {
let claim: Claim = {
"email": username,
"errors": [],
"successMessage": null
}
let body = JSON.stringify(claim)

return this._http
.post<Claim>(environment.API_WEB + `resend-claim.json`, body, {
headers: new HttpHeaders(
{
'Access-Control-Allow-Origin':'*',
'Content-Type': 'application/json'
}),
withCredentials: true,
})
.pipe(
retry(3),
catchError((error) =>
this._errorHandler.handleError(
error,
ERROR_REPORT.REGISTER_REACTIVATED_EMAIL
)
)
)
}

singOut() {
this._titleService.setTitle('ORCID')
return this._http
.get<SignIn>(environment.API_WEB + 'userStatus.json?logUserOut=true', {
withCredentials: true,
})
.pipe(
retry(3),
catchError((error) => this._errorHandler.handleError(error)),
switchMap(() => this._userService.refreshUserSession())
)
}
}
Loading
Loading