Skip to content
Snippets Groups Projects
Commit 801ad014 authored by Mihail Gorceag's avatar Mihail Gorceag
Browse files

feat: configure redirect for the first time visit user

parent 74e72693
No related branches found
No related tags found
No related merge requests found
File moved
import React, { useState } from 'react'
import React from 'react'
import { Redirect } from 'react-router-dom'
import config from 'config'
import { th, grid, lighten } from '@pubsweet/ui-toolkit'
import { Button } from '@pubsweet/ui'
import styled from 'styled-components'
import getQueryStringByName from '../../../shared/getQueryStringByName'
import brandConfig from '../../../brandConfig.json'
const getNextUrl = () => {
......@@ -20,16 +20,6 @@ const getNextUrl = () => {
return `${url.searchParams.get('next') || redirectLink}`
}
const getToken = props => {
const { location } = props
if (location && location.search && location.search.match(/^\?token=/)) {
return location.search.replace(/^\?token=/, '')
}
return null
}
const LoginButton = styled(Button)`
display: block;
margin-top: ${grid(3)};
......@@ -101,12 +91,11 @@ const StyledORCIDIcon = styled(ORCIDIcon)`
`
const Login = ({ logo = null, ...props }) => {
const token = getToken(props)
const token = getQueryStringByName('token')
// If a JWT token is supplied as a query param (e.g. from OAuth)
// go ahead and fetch the redirect URL
const initialRedirectLink = token ? getNextUrl() : null
const [redirectLink] = useState(initialRedirectLink)
let redirectLink = token ? getNextUrl() : null
redirectLink = (redirectLink && getQueryStringByName('redirectUrl')) ? getQueryStringByName('redirectUrl') : redirectLink
if (token) {
window.localStorage.setItem('token', token)
return <Redirect to={redirectLink} />
......
function getParameterByName(name, url = window.location.href) {
const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`)
const results = regex.exec(url)
if (!results) return null
if (!results[2]) return ''
return decodeURIComponent(results[2].replace(/\+/g, ' '))
}
export default getParameterByName
......@@ -31,6 +31,7 @@ module.exports = app => {
.where('identities.type', 'orcid')
.throwIfNotFound()
.first()
user.firstLogin = false
} catch (err) {
// swallow not found error
if (err.name !== 'NotFoundError') {
......@@ -57,6 +58,7 @@ module.exports = app => {
user.defaultIdentity.name = `${userDetails.firstName} ${userDetails.lastName}`
user.defaultIdentity.aff = userDetails.institution
user.firstLogin = true
user.saveGraph()
}
} catch (err) {
......@@ -81,7 +83,11 @@ module.exports = app => {
}),
(req, res) => {
const jwt = authentication.token.create(req.user)
res.redirect(`/login?token=${jwt}`)
res.redirect(
`/login?token=${jwt}&redirectUrl=${
req.user.firstLogin ? '/kotahi/profile' : '/kotahi/dashboard'
}`,
)
},
)
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment