-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add express login link to furever (#211)
* Add express login link to furever * Refactor to use hook * Clean up imports
- Loading branch information
1 parent
8fa34a5
commit fee19df
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {getServerSession} from 'next-auth/next'; | ||
import {authOptions} from '@/lib/auth'; | ||
import {stripe} from '@/lib/stripe'; | ||
|
||
export async function GET() { | ||
try { | ||
const session = await getServerSession(authOptions); | ||
|
||
const stripeAccount = session?.user?.stripeAccount?.id; | ||
if (!stripeAccount) { | ||
console.error('No connected account found for user'); | ||
return new Response('No connected account found for user', { | ||
status: 400, | ||
}); | ||
} | ||
|
||
if ( | ||
session.user.stripeAccount.controller?.stripe_dashboard?.type !== | ||
'express' | ||
) { | ||
console.error('User does not have access to Express dashboard'); | ||
return new Response('User does not have access to Express dashboard.', { | ||
status: 400, | ||
}); | ||
} | ||
|
||
const link = await stripe.accounts.createLoginLink(stripeAccount); | ||
|
||
return new Response( | ||
JSON.stringify({ | ||
url: link.url, | ||
}), | ||
{status: 200, headers: {'Content-Type': 'application/json'}} | ||
); | ||
} catch (error: any) { | ||
console.error( | ||
'An error occurred when calling the Stripe API to create a login link', | ||
error | ||
); | ||
return new Response(error.message, {status: 500}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {useSession} from 'next-auth/react'; | ||
import {useEffect, useState} from 'react'; | ||
|
||
export const useExpressDashboardLoginLink = () => { | ||
const {data: session} = useSession(); | ||
|
||
const hasExpressDashboardAccess = | ||
session?.user?.stripeAccount?.controller?.stripe_dashboard?.type === | ||
'express'; | ||
|
||
const [expressDashboardLoginLink, setExpressDashboardLoginLink] = | ||
useState<string>(); | ||
|
||
useEffect(() => { | ||
const fetchLink = async () => { | ||
const res = await fetch('/api/login_link'); | ||
const data = await res.json(); | ||
setExpressDashboardLoginLink(data.url); | ||
}; | ||
|
||
fetchLink(); | ||
}, []); | ||
|
||
return {hasExpressDashboardAccess, expressDashboardLoginLink}; | ||
}; |