Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try implementing auth using AWS Cognito #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions functions/auth/post_auth_code_for_tokens/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const clientId = process.env.COGNITO_CLIENTID
const clientSecret = process.env.COGNITO_CLIENTSECRET
const redirectURI = ''
const tokenEndpoint = 'https://auth.openshelf.willemhandreck.com/oauth2/token'

export const handler = async (event) => {
const code = event.queryStringParameters.code

const data = {
grant_type: 'authorization_code'
}


try {
const result = await client.query(
'SELECT * FROM books WHERE id=$1::int',
[event.bookId]
)
return result.rows[0]
}
catch (err) {
console.error('Postgres query error', err.stack)
return {}
}
}
5 changes: 5 additions & 0 deletions web-client/src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
RouterProvider,
} from "react-router-dom";

import Auth from "./pages/Auth";
import Book from "./pages/Book";
import Home from "./pages/Home";

Expand All @@ -14,6 +15,10 @@ const router = createBrowserRouter([
{
path: "/books/:bookId",
element: <Book />,
},
{
path: "/auth",
element: <Auth />
}
])

Expand Down
40 changes: 40 additions & 0 deletions web-client/src/pages/Auth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";

const baseURL = 'https://wxt9njnxk9.execute-api.ap-southeast-2.amazonaws.com/dev/'

function Auth() {
const navigate = useNavigate();
const location = useLocation();
const hashParams = new URLSearchParams(location.search);
const code = hashParams.get('code');
console.log('Location:', location)
console.log('Code:', code)

// const fetchTokens = async () => {
// try {
// const response = await fetch(baseURL + 'auth', {
// method: 'POST',
// headers: {
// "Content-Type": "application/json"
// },
// body: JSON.stringify({ code })
// })
// const data = await response.json()
// const accessToken = data.get('accessToken')
// console.log('Access token', accessToken)
// }
// catch (error) {
// console.error(error)
// }
// }

useEffect(() => {
// fetchTokens()
navigate('/')
}, [])

return <>Signing in...</>
}

export default Auth
2 changes: 1 addition & 1 deletion web-client/src/pages/Book.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react'
import { Link, useParams } from 'react-router-dom'

const baseURL = 'https://wxt9njnxk9.execute-api.ap-southeast-2.amazonaws.com/dev/'
const baseURL = 'https://api.openshelf.willemhandreck.com/'
const initialState = {
title: 'Loading...',
author: ''
Expand Down
6 changes: 5 additions & 1 deletion web-client/src/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useEffect, useState } from 'react'
import { Link } from 'react-router-dom'

const baseURL = 'https://wxt9njnxk9.execute-api.ap-southeast-2.amazonaws.com/dev/'
const baseURL = 'https://api.openshelf.willemhandreck.com/'
// const redirectURI = 'https://auth.openshelf.willemhandreck.com/oauth2/authorize?client_id=fctdccmihu7nns0ji0dbggnjk&response_type=code&scope=email+openid+phone&redirect_uri=https%3A%2F%2Fopenshelf.willemhandreck.com'
const redirectURI = 'https://auth.openshelf.willemhandreck.com/oauth2/authorize?client_id=fctdccmihu7nns0ji0dbggnjk&response_type=token&scope=email+openid+phone&redirect_uri=http%3A%2F%2Flocalhost:5173/auth'

function Home() {
const [books, setBooks] = useState([])
Expand All @@ -24,6 +26,8 @@ function Home() {
return (
<div className="h-screen bg-slate-100">

<Link to={redirectURI}>Sign In</Link>

<h1 className="text-2xl">Books</h1>
<ul className="bg-white">
{
Expand Down