feat(SW-3541): Do social login after login to SAS * feat(auth): wip social login via curity * Setup social login auth flow * Merge branch 'master' of bitbucket.org:scandic-swap/web into feature/curity-social-login * Added support for getting scandic tokens and refresh them * feat: Enhance social login and session management with auto-refresh and improved error handling * Merge branch 'master' of bitbucket.org:scandic-swap/web into feature/curity-social-login * wrap layout in suspense * revert app/layout.tsx * fix import * cleanup * merge * merge * dont pass client_secret in the url to curity * add state validation when doing social login through /authorize * remove debug logging Approved-by: Anton Gunnarsson
60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
"use client"
|
|
|
|
import React, { createContext, useContext } from "react"
|
|
|
|
import { useSocialSession } from "@/hooks/useSocialSession"
|
|
|
|
/* eslint-disable formatjs/no-literal-string-in-jsx */
|
|
|
|
type SocialLoginContextType = {
|
|
session: ReturnType<typeof useSocialSession>["session"]
|
|
refresh: ReturnType<typeof useSocialSession>["refresh"]
|
|
}
|
|
|
|
const SocialLoginContext = createContext<SocialLoginContextType | undefined>(
|
|
undefined
|
|
)
|
|
|
|
export function SocialLoginProvider({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode
|
|
}) {
|
|
const { session: socialSession, refresh } = useSocialSession()
|
|
|
|
return (
|
|
<SocialLoginContext.Provider value={{ session: socialSession, refresh }}>
|
|
{process.env.NODE_ENV === "development" && (
|
|
<>
|
|
<span>Social login: </span>
|
|
{socialSession.data?.status}{" "}
|
|
{socialSession?.data?.status === "expired" && (
|
|
<>
|
|
<span>Expires@{socialSession?.data.expiresAt}</span>{" "}
|
|
<button onClick={() => refresh.mutate()}>Refresh</button>
|
|
</>
|
|
)}
|
|
{socialSession?.data?.status === "valid" && (
|
|
<>
|
|
<span>Expires@{socialSession?.data.expiresAt}</span>{" "}
|
|
<button onClick={() => refresh.mutate()}>Refresh</button>
|
|
</>
|
|
)}
|
|
</>
|
|
)}
|
|
|
|
{children}
|
|
</SocialLoginContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useSocialLogin() {
|
|
const ctx = useContext(SocialLoginContext)
|
|
if (!ctx) {
|
|
throw new Error(
|
|
"useSocialLogin must be used within SocialLoginContextProvider"
|
|
)
|
|
}
|
|
return ctx
|
|
}
|