First Commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import React, { useEffect, useState } from "react";
|
||||
|
||||
import { Box, Button, Paper, Typography } from "@mui/material";
|
||||
|
||||
import { api } from "../api";
|
||||
import { clearAuthToken, getAuthToken } from "../auth";
|
||||
import { useToast } from "../toast";
|
||||
|
||||
type MeResponse = {
|
||||
provider?: string;
|
||||
id?: string;
|
||||
email?: string;
|
||||
userName?: string;
|
||||
roles?: string[];
|
||||
};
|
||||
|
||||
export default function AuthStatusCard() {
|
||||
const { toast } = useToast();
|
||||
const token = getAuthToken();
|
||||
const [me, setMe] = useState<MeResponse | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!token) {
|
||||
setMe(null);
|
||||
return;
|
||||
}
|
||||
api
|
||||
.get<MeResponse>("/auth/me")
|
||||
.then((r) => setMe(r.data))
|
||||
.catch(() => setMe(null));
|
||||
}, [token]);
|
||||
|
||||
return (
|
||||
<Paper sx={{ mt: 2, p: 2 }}>
|
||||
<Typography variant="h6" sx={{ mb: 1 }}>
|
||||
Authentication
|
||||
</Typography>
|
||||
|
||||
{!token ? (
|
||||
<Typography sx={{ color: "text.secondary" }}>Not signed in.</Typography>
|
||||
) : (
|
||||
<Box sx={{ display: "flex", flexDirection: "column", gap: 0.5 }}>
|
||||
<Typography sx={{ color: "text.secondary" }}>
|
||||
Signed in{me?.email ? ` as ${me.email}` : ""}{me?.provider ? ` (${me.provider})` : ""}.
|
||||
</Typography>
|
||||
{me?.roles && me.roles.length > 0 ? (
|
||||
<Typography sx={{ color: "text.secondary" }}>
|
||||
Roles: {me.roles.join(", ")}
|
||||
</Typography>
|
||||
) : null}
|
||||
|
||||
<Box sx={{ mt: 1 }}>
|
||||
<Button
|
||||
variant="outlined"
|
||||
onClick={() => {
|
||||
clearAuthToken();
|
||||
setMe(null);
|
||||
toast("Signed out.", "info");
|
||||
}}
|
||||
>
|
||||
Sign out
|
||||
</Button>
|
||||
</Box>
|
||||
</Box>
|
||||
)}
|
||||
</Paper>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user