-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #483 from sinamics/azure
Azure AD Oauth Authentication
- Loading branch information
Showing
8 changed files
with
163 additions
and
70 deletions.
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
3 changes: 3 additions & 0 deletions
3
prisma/migrations/20240809164834_oauth_provider/migration.sql
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,3 @@ | ||
-- AlterTable | ||
ALTER TABLE "Account" ADD COLUMN "expires_in" INTEGER, | ||
ADD COLUMN "ext_expires_in" INTEGER; |
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
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 |
---|---|---|
@@ -1,44 +1,80 @@ | ||
import { signIn } from "next-auth/react"; | ||
import { signIn, getProviders } from "next-auth/react"; | ||
import { useRouter } from "next/router"; | ||
import { useState } from "react"; | ||
import { useState, useEffect } from "react"; | ||
import { toast } from "react-hot-toast"; | ||
import cn from "classnames"; | ||
|
||
const OauthLogin: React.FC = () => { | ||
interface OAuthProvider { | ||
id: string; | ||
name: string; | ||
type: string; | ||
signinUrl: string; | ||
callbackUrl: string; | ||
} | ||
|
||
const OAuthLogin: React.FC = () => { | ||
const router = useRouter(); | ||
const { error: oauthError } = router.query; | ||
const [loading, setLoading] = useState(false); | ||
const [providers, setProviders] = useState<Record<string, OAuthProvider>>({}); | ||
|
||
useEffect(() => { | ||
const fetchProviders = async () => { | ||
try { | ||
const fetchedProviders = await getProviders(); | ||
|
||
if (fetchedProviders) { | ||
setProviders(fetchedProviders); | ||
} | ||
} catch (error) { | ||
console.error("Failed to fetch providers:", error); | ||
toast.error("Failed to load login options"); | ||
} | ||
}; | ||
|
||
fetchProviders(); | ||
}, []); | ||
|
||
const oAuthHandler = async (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => { | ||
event.preventDefault(); | ||
const oAuthHandler = async (providerId: string) => { | ||
setLoading(true); | ||
|
||
try { | ||
await signIn("oauth"); | ||
if (!oauthError) { | ||
const result = await signIn(providerId, { redirect: false }); | ||
|
||
if (result?.error) { | ||
toast.error(`Error occurred: ${result.error}`, { duration: 10000 }); | ||
} else if (result?.ok) { | ||
await router.push("/network"); | ||
} else { | ||
toast.error(`Error occurred: ${oauthError}` as string, { duration: 10000 }); | ||
} | ||
} catch (_error) { | ||
toast.error(`Error occurred: ${oauthError}` as string); | ||
toast.error("Unexpected error occurred", { duration: 10000 }); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
// Convert providers object to array and sort by name | ||
const sortedProviders = Object.values(providers) | ||
.filter((provider) => provider.type === "oauth") | ||
.sort((a, b) => a.name.localeCompare(b.name)); | ||
return ( | ||
<button | ||
type="button" | ||
onClick={oAuthHandler} | ||
className={cn( | ||
"btn btn-block btn-primary cursor-pointer font-semibold tracking-wide shadow-lg", | ||
)} | ||
> | ||
{loading ? <span className="loading loading-spinner"></span> : null} | ||
Sign in with OAuth | ||
</button> | ||
<div> | ||
{sortedProviders.map((provider) => ( | ||
<button | ||
key={provider.id} | ||
type="button" | ||
onClick={() => oAuthHandler(provider.id)} | ||
className={cn( | ||
"btn btn-block btn-primary cursor-pointer font-semibold tracking-wide shadow-lg mb-2", | ||
{ "opacity-50 cursor-not-allowed": loading }, | ||
)} | ||
disabled={loading} | ||
> | ||
{loading ? <span className="loading loading-spinner"></span> : null} | ||
Sign in with {provider.name} | ||
</button> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
export default OauthLogin; | ||
export default OAuthLogin; |
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
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