From 4aa9c691895d332ea7ac72928d55f5040511192c Mon Sep 17 00:00:00 2001 From: Marc Farra Date: Wed, 7 Jun 2023 20:15:53 +0300 Subject: [PATCH 1/3] Provides a page for when user is not signed in --- src/pages/api/auth/[...nextauth].js | 4 +++ src/pages/login.js | 37 ------------------------ src/pages/signin.js | 45 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 37 deletions(-) delete mode 100644 src/pages/login.js create mode 100644 src/pages/signin.js diff --git a/src/pages/api/auth/[...nextauth].js b/src/pages/api/auth/[...nextauth].js index 519bda6e..34c2175e 100644 --- a/src/pages/api/auth/[...nextauth].js +++ b/src/pages/api/auth/[...nextauth].js @@ -40,6 +40,10 @@ export const authOptions = { }, }, + pages: { + signIn: '/signin', + }, + events: { async signIn({ profile }) { // On successful sign in we should persist the user to the database diff --git a/src/pages/login.js b/src/pages/login.js deleted file mode 100644 index 16b6a553..00000000 --- a/src/pages/login.js +++ /dev/null @@ -1,37 +0,0 @@ -import React, { Component } from 'react' -import { Box, Button, Container, Heading } from '@chakra-ui/react' -import Link from 'next/link' - -const OSM_NAME = process.env.OSM_NAME -class Login extends Component { - static async getInitialProps({ query }) { - if (query) { - return { - challenge: query.challenge, - } - } - } - - render() { - return ( - - - Login -

- Teams uses {OSM_NAME} as your login, connect your {OSM_NAME}{' '} - account! -

-
- -
-
- ) - } -} - -export default Login diff --git a/src/pages/signin.js b/src/pages/signin.js new file mode 100644 index 00000000..32d8dbc9 --- /dev/null +++ b/src/pages/signin.js @@ -0,0 +1,45 @@ +import { signIn, getProviders } from 'next-auth/react' +import { getServerSession } from 'next-auth/next' +import { Box, Container, Heading, Text, Button } from '@chakra-ui/react' +import InpageHeader from '../components/inpage-header' +import { authOptions } from './api/auth/[...nextauth]' + +export default function SignIn() { + return ( + <> + + + + You are not signed in. + + + + + + Sorry, you need to be signed in to view this page. + + + Still having problems? Contact a system administrator. + + + + + ) +} + +export async function getServerSideProps(context) { + const session = await getServerSession(context.req, context.res, authOptions) + + // If the user is already logged in, redirect. + // Note: Make sure not to redirect to the same page + // To avoid an infinite loop! + if (session) { + return { redirect: { destination: '/' } } + } + + const providers = await getProviders() + + return { + props: { providers: providers ?? [] }, + } +} From fb9180b3ff72e23962c5ee9a3fd38bbb7d8055f7 Mon Sep 17 00:00:00 2001 From: LanesGood Date: Thu, 8 Jun 2023 16:36:51 -0400 Subject: [PATCH 2/3] Add button margin --- src/pages/signin.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pages/signin.js b/src/pages/signin.js index 32d8dbc9..9995e1f2 100644 --- a/src/pages/signin.js +++ b/src/pages/signin.js @@ -18,7 +18,9 @@ export default function SignIn() { Sorry, you need to be signed in to view this page. - + Still having problems? Contact a system administrator. From e7874cff0d56cfce7ffdea3cd97ae136cd1d8c63 Mon Sep 17 00:00:00 2001 From: Marc Farra Date: Fri, 9 Jun 2023 17:23:03 -0400 Subject: [PATCH 3/3] Fix tests --- cypress/e2e/auth.cy.js | 2 +- cypress/e2e/organizations/permissions.cy.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cypress/e2e/auth.cy.js b/cypress/e2e/auth.cy.js index 3b1cf6cb..57290eaf 100644 --- a/cypress/e2e/auth.cy.js +++ b/cypress/e2e/auth.cy.js @@ -21,7 +21,7 @@ describe('Check protected routes', () => { protectedRoutes.forEach((testRoute) => { it(`Route ${testRoute} needs authentication`, () => { cy.visit(testRoute) - cy.get('body').should('contain', 'Sign in with OSM Teams') + cy.get('body').should('contain', 'Sign in') }) }) diff --git a/cypress/e2e/organizations/permissions.cy.js b/cypress/e2e/organizations/permissions.cy.js index dca3237a..ec3f5d1f 100644 --- a/cypress/e2e/organizations/permissions.cy.js +++ b/cypress/e2e/organizations/permissions.cy.js @@ -67,7 +67,7 @@ describe('Organizations page: Permissions', () => { it('Org is private', () => { // Unauthenticated user cannot access cy.visit('/organizations/1') - cy.get('body').should('contain', 'Sign in with OSM Teams') + cy.get('body').should('contain', 'Sign in') // Non-member cannot access cy.login(nonMember) @@ -142,7 +142,7 @@ describe('Organizations page: Permissions', () => { // Unauthenticated can view org cy.visit('/organizations/2') - cy.get('body').should('contain', 'Sign in with OSM Teams') + cy.get('body').should('contain', 'Sign in') // Non-member can access, but cannot view private teams cy.login(nonMember)