Skip to content

Commit

Permalink
feat: signup also saves into list of previous logged in orgs (#3165)
Browse files Browse the repository at this point in the history
* feat: prelogin page that lists orgs that were logged into, using cookies

* improve: small

* improve: origin to host, use //

* improve: small

* improve: small

* improve: push package.json

* improve: reorganized imports

* improve: small

* improve: small

* improve: small

* improve: small

* improve: small

* feat: signup also saves into list of previous logged in orgs

* improve: helper fn

* improve: small

Co-authored-by: Bruce MacDonald <brucewmacdonald@gmail.com>

Co-authored-by: Bruce MacDonald <brucewmacdonald@gmail.com>
  • Loading branch information
2 people authored and jmorganca committed Sep 19, 2022
1 parent 7f976f5 commit b0c9000
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
16 changes: 2 additions & 14 deletions ui/pages/login/callback.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useEffect } from 'react'
import { useRouter } from 'next/router'
import { useSWRConfig } from 'swr'
import Cookies from 'universal-cookie'

import { useServerConfig } from '../../lib/serverconfig'
import { saveToVisitedOrgs } from '.'

import LoginLayout from '../../components/layouts/login'

Expand All @@ -16,8 +16,6 @@ export default function Callback() {
const { code, state } = router.query

useEffect(() => {
const cookies = new Cookies()

async function login({ providerID, code, redirectURL, next }) {
await fetch('/api/login', {
method: 'POST',
Expand All @@ -39,17 +37,7 @@ export default function Callback() {
}
window.localStorage.removeItem('next')

let visitedOrgs = cookies.get('orgs') || []
if (!visitedOrgs.find(x => x.url === window.location.host)) {
visitedOrgs.push({
url: window.location.host,
})

cookies.set('orgs', visitedOrgs, {
path: '/',
domain: `.${baseDomain}`,
})
}
saveToVisitedOrgs(window.location.host, baseDomain)
}

const providerID = window.localStorage.getItem('providerID')
Expand Down
29 changes: 17 additions & 12 deletions ui/pages/login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ function oidcLogin({ id, clientID, authURL, scopes }, next) {
)}&state=${state}`
}

export function saveToVisitedOrgs(domain, baseDomain) {
const cookies = new Cookies()

let visitedOrgs = cookies.get('orgs') || []
if (!visitedOrgs.find(x => x.url === domain)) {
visitedOrgs.push({
url: domain,
})

cookies.set('orgs', visitedOrgs, {
path: '/',
domain: `.${baseDomain}`,
})
}
}

export function Providers({ providers }) {
const router = useRouter()
const { next } = router.query
Expand Down Expand Up @@ -81,7 +97,6 @@ export default function Login() {
const [password, setPassword] = useState('')
const [error, setError] = useState('')
const { baseDomain, isEmailConfigured } = useServerConfig()
const cookies = new Cookies()

async function onSubmit(e) {
e.preventDefault()
Expand Down Expand Up @@ -114,17 +129,7 @@ export default function Login() {

await mutate('/api/users/self')
router.replace('/')
let visitedOrgs = cookies.get('orgs') || []

if (!visitedOrgs.find(x => x.url === window.location.host)) {
visitedOrgs.push({
url: window.location.host,
})
}
cookies.set('orgs', visitedOrgs, {
path: '/',
domain: `.${baseDomain}`,
})
saveToVisitedOrgs(window.location.host, baseDomain)
} catch (e) {
console.error(e)
setError('Invalid credentials')
Expand Down
2 changes: 2 additions & 0 deletions ui/pages/signup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useServerConfig } from '../../lib/serverconfig'

import Login from '../../components/layouts/login'
import ErrorMessage from '../../components/error-message'
import { saveToVisitedOrgs } from '../login'

export default function Signup() {
const [name, setName] = useState('')
Expand Down Expand Up @@ -43,6 +44,7 @@ export default function Signup() {
let created = await res.json()

window.location = `${window.location.protocol}//${created?.organization?.domain}`
saveToVisitedOrgs(`${created?.organization?.domain}`, baseDomain)
} catch (e) {
setSubmitted(false)
if (e.fieldErrors) {
Expand Down

0 comments on commit b0c9000

Please sign in to comment.