mirror of
https://github.com/System-End/site.git
synced 2026-04-19 22:05:11 +00:00
Add airtable based login
This commit is contained in:
parent
15fd24b975
commit
eea0315254
2 changed files with 78 additions and 0 deletions
53
pages/api/arcade/showcase/login/[token].js
Normal file
53
pages/api/arcade/showcase/login/[token].js
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import AirtablePlus from "airtable-plus"
|
||||
|
||||
const airtable = new AirtablePlus({
|
||||
apiKey: process.env.AIRTABLE_API_KEY,
|
||||
baseID: 'app4kCWulfB02bV8Q',
|
||||
tableName: "Users"
|
||||
})
|
||||
|
||||
async function getUserFromLogin(loginToken) {
|
||||
|
||||
// only alphanumeric & '-' characters are allowed in the token
|
||||
const safeLoginToken = loginToken.replace(/[^a-zA-Z0-9-]/g, '')
|
||||
|
||||
const results = await airtable.read({
|
||||
filterByFormula: `{Login Token} = '${safeLoginToken}'`,
|
||||
maxRecords: 1
|
||||
})
|
||||
|
||||
return results[0]
|
||||
}
|
||||
|
||||
async function scrubLoginToken(userID) {
|
||||
console.log(`Scrubbing login token for user ${userID}`)
|
||||
await airtable.update(userID, {
|
||||
'Login Token': ''
|
||||
})
|
||||
}
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method !== 'POST') {
|
||||
return res.status(405).json({ error: "Method not allowed" })
|
||||
}
|
||||
|
||||
const { token } = req.query
|
||||
if (!token) {
|
||||
return res.status(400).json({ error: "Token is required" })
|
||||
}
|
||||
|
||||
const user = await getUserFromLogin(token)
|
||||
if (!user) {
|
||||
return res.status(404).json({ error: "User not found" })
|
||||
}
|
||||
|
||||
const authToken = user.fields['Auth Token']
|
||||
if (!authToken) {
|
||||
return res.status(500).json({ error: "Auth Token not found" })
|
||||
}
|
||||
|
||||
await scrubLoginToken(user.id)
|
||||
|
||||
// return back the user's AuthToken
|
||||
res.status(200).json({ authToken })
|
||||
}
|
||||
25
pages/arcade/showcase/login/[token].js
Normal file
25
pages/arcade/showcase/login/[token].js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { useEffect, useState } from 'react'
|
||||
const LoginPage = ({token}) => {
|
||||
const [ status, setStatus ] = useState('loading')
|
||||
useEffect(async () => {
|
||||
const response = await fetch(`/api/arcade/showcase/login/${token}`, {method: 'POST'})
|
||||
const data = await response.json()
|
||||
setStatus(data.error || data.authToken)
|
||||
if (data.authToken) {
|
||||
window.localStorage.setItem('arcade.authToken', data.authToken)
|
||||
window.location.href = '/arcade/showcase/my'
|
||||
}
|
||||
}, [])
|
||||
return (
|
||||
<div>
|
||||
<h1>{status}</h1>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default LoginPage
|
||||
|
||||
export function getServerSideProps(context) {
|
||||
const { token } = context.query
|
||||
return { props: { token } }
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue