Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit

Permalink
change: デモ用に認証なしで利用できるように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
testusuke committed Oct 14, 2024
1 parent 9cc161f commit 2cd7864
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 48 deletions.
21 changes: 6 additions & 15 deletions app/api/auth/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import {NextRequest} from "next/server"

export async function POST(request: NextRequest) {
// form data
const form = await request.formData()
// get state and code
const code = form.get('code')

// pass code to the backend
const loginEndpoint = process.env.NEXT_PUBLIC_API_URL + '/login'

// redirect uri
const redirectUri = process.env.NEXT_PUBLIC_OIDC_REDIRECT_URL
const backendURL = process.env.NEXT_PUBLIC_API_INTERNAL_URL ? process.env.NEXT_PUBLIC_API_INTERNAL_URL : process.env.NEXT_PUBLIC_API_URL
const loginEndpoint = backendURL + '/login'

// post code to the backend using fetch
const response = await fetch(loginEndpoint, {
Expand All @@ -19,20 +12,18 @@ export async function POST(request: NextRequest) {
'Content-Type': 'application/json',
},
body: JSON.stringify({
code: code,
redirect_uri: redirectUri
}),
})
})

// get cookie from response
const cookie = response.headers.get('set-cookie')

const subDirectory = process.env.SUB_DIRECTORY ? process.env.SUB_DIRECTORY : "/"
if (cookie) {
// redirect to root page
return new Response(null, {
status: 301,
headers: {
"Location": '/',
"Location": subDirectory,
"Set-Cookie": cookie,
},
})
Expand All @@ -42,7 +33,7 @@ export async function POST(request: NextRequest) {
return new Response(null, {
status: 301,
headers: {
"Location": '/',
"Location": subDirectory,
},
})
}
49 changes: 17 additions & 32 deletions components/auth/LoginButton.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,37 @@
'use client'
import {Button} from "@mui/material";
import crypto from 'crypto';
import * as querystring from "querystring";
import {useEffect, useState} from "react";
import * as React from "react";
import {useRouter} from "next/navigation";

export default function LoginButton() {
const [authorizationUrl, setAuthorizationUrl] = useState<string>('')
const subDirectory = process.env.SUB_DIRECTORY ? process.env.SUB_DIRECTORY : "/"
const router = useRouter()

useEffect(() => {
const authorizationBaseUrl = process.env.NEXT_PUBLIC_OIDC_AUTHORIZE_URL
// query params
const clientId = process.env.NEXT_PUBLIC_OIDC_CLIENT_ID
const redirectUri = process.env.NEXT_PUBLIC_OIDC_REDIRECT_URL
const scope = process.env.NEXT_PUBLIC_OIDC_SCOPE ?? "openid profile email"
// generate random nonce and state
const nonce = crypto.randomBytes(16).toString('hex')

const queryData = {
"client_id": clientId,
"redirect_uri": redirectUri,
"response_type": "code",
"response_mode": "form_post",
"scope": scope,
"nonce": nonce,
}

// make query string
const searchParams = querystring.stringify(queryData);
// make url
setAuthorizationUrl(`${authorizationBaseUrl}?${searchParams}`)
}, [])

// display name
const buttonDisplayName = process.env.NEXT_PUBLIC_OIDC_DISPLAY_NAME ?? "Login"
const handleLogin = async () => {
await fetch("/api/auth/callback", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({})
})
router.replace(subDirectory)
}

return (
<Button
variant="contained"
href={authorizationUrl}
onClick={handleLogin}
disableElevation
sx={{
width: "100%",
color: 's-dark.main',
backgroundColor: 's-light.main',
borderRadius: "9px",
py: 1.5
}}
>
{buttonDisplayName}
ログイン
</Button>
);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "sports-day-form",
"name": "sports-day-form-procon",
"version": "0.1.0",
"private": true,
"scripts": {
Expand Down

0 comments on commit 2cd7864

Please sign in to comment.