Skip to content

Commit

Permalink
Logout route
Browse files Browse the repository at this point in the history
  • Loading branch information
Keskimaki committed Aug 25, 2023
1 parent 0e7c8ac commit 2db3d27
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions compose.ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
image: test
environment:
- DATABASE_URL=mongodb://root:root@db/admin
- SESSION_SECRET=secret
ports:
- 8000:8000
container_name: test
Expand Down
5 changes: 3 additions & 2 deletions src/client/components/NavBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { Language, LoginOutlined, LogoutOutlined } from '@mui/icons-material'
import { useTranslation } from 'react-i18next'

import { HOST_NAME } from '../../../config'
import useCurrentUser from '../../hooks/useCurrentUser'
import hyLogo from '../../assets/hy_logo.svg'
import styles from './styles'
Expand Down Expand Up @@ -56,7 +57,7 @@ const NavBar = () => {
<Box>
{user ? (
<Link
href="https://openid.ext.ocp-test-0.k8s.it.helsinki.fi/api/logout"
href={`${HOST_NAME}/api/logout`}
style={{ textDecoration: 'none' }}
>
<Button>
Expand All @@ -65,7 +66,7 @@ const NavBar = () => {
</Link>
) : (
<Link
href="https://openid.ext.ocp-test-0.k8s.it.helsinki.fi/api/login"
href={`${HOST_NAME}/api/oidc`}
style={{ textDecoration: 'none' }}
>
<Button>
Expand Down
2 changes: 1 addition & 1 deletion src/client/hooks/useCurrentUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const queryKey = ['login']

const useCurrentUser = () => {
const queryFn = async (): Promise<User | null> => {
const res = await fetch(`${BASE_PATH}/api/users`)
const res = await fetch(`${BASE_PATH}/api/login`)

if (res.status === 401) return null

Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export const inProduction = !inStaging && process.env.NODE_ENV === 'production'
export const inE2EMode = process.env.E2E === 'true'

export const BASE_PATH = process.env.BASE_PATH || ''

export const HOST_NAME = process.env.HOST_NAME || 'http://localhost:3000'
2 changes: 0 additions & 2 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import cors from 'cors'
import { inDevelopment, inE2EMode } from '../../config'
import userMiddleware from '../middleware/user'
import loginRouter from './login'
import userRouter from './user'
import counterRouter from './counter'

const router = express()
Expand All @@ -19,7 +18,6 @@ if (inDevelopment || inE2EMode) {
router.get('/ping', (_, res) => res.send('pong'))

router.use('/', loginRouter)
router.use('/users', userRouter)
router.use('/counter', counterRouter)

export default router
20 changes: 20 additions & 0 deletions src/server/routes/login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from 'express'
import passport from 'passport'

import { RequestWithUser } from '../types'

Expand All @@ -12,4 +13,23 @@ loginRouter.get('/login', async (req, res) => {
return res.send(user)
})

loginRouter.get('/oidc', passport.authenticate('oidc'))

loginRouter.get(
'/callback',
passport.authenticate('oidc', { failureRedirect: '/' }),
(_, res) => {
res.redirect('/')
}
)

loginRouter.get('/logout', async (req, res, next) => {
// eslint-disable-next-line consistent-return
req.logout((err) => {
if (err) return next(err)
})

res.redirect('/')
})

export default loginRouter
15 changes: 0 additions & 15 deletions src/server/routes/user.ts

This file was deleted.

0 comments on commit 2db3d27

Please sign in to comment.