I am wondering how do i get the logged in user information I see the token being stored in cookies but I don’t see any user info coming back for auth.
import { useState } from 'react';
import { Navigate } from 'react-router-dom';
import { auth } from '@/client';
import { Alert, Button, Stack, Typography } from '@mui/material';
import { useMutation } from '@tanstack/react-query';
import { BrandLogo } from '@/components/BrandLogo';
import UnauthenticatedLayout from '@/components/Layouts/UnauthenticatedLayout';
export default function Login() {
const [isLoggingIn, setIsLoggingIn] = useState(false);
const [error, setError] = useState<string | undefined>(undefined);
const token = auth.getTokenOrUndefined();
const mutation = useMutation({
mutationFn: () => auth.signIn(),
onSuccess: () => {
setIsLoggingIn(false);
},
onError: (e: Error) => {
setIsLoggingIn(false);
setError(e.message ?? e);
},
});
const handleLogin = () => {
setIsLoggingIn(true);
mutation.mutate();
};
// If the token exists but a user tries to load /login, redirect to the home page instead
if (token != null) {
return <Navigate to="/" replace={true} />;
}
return (
<UnauthenticatedLayout>
<Stack
sx={{
background: (theme) => theme.palette.common.white,
borderRadius: 2,
boxShadow: 1,
width: 1,
maxWidth: 450,
p: 3,
}}
gap={3}
alignItems="center"
>
<BrandLogo />
<Typography component="h1" variant="h2" sx={{ m: 0, p: 0 }}>
Welcome Back!
</Typography>
{error && (
<Alert
variant="outlined"
severity="error"
sx={{ width: 1, background: (theme) => theme.palette.error.light }}
>
<Typography variant="body2" color="error">
{error}
</Typography>
</Alert>
)}
<Button
onClick={handleLogin}
variant="contained"
loading={isLoggingIn}
loadingPosition="start"
sx={{ width: 1 }}
>
AD Login
</Button>
</Stack>
</UnauthenticatedLayout>
);
}
here is my code auth.signIn
just returns token info nothing about the user.
Hi,
The code snippet you posted above only handles user login and authentication.
To fetch user details, you can use the platform sdk admin package, installed via:
npm install @osdk/foundry.admin
Assuming you have a client initiated:
import { Client, createClient } from "@osdk/client";
import { createPublicOauthClient, PublicOauthClient} from "@osdk/oauth";
import { $ontologyRid } from "@<YOUR_SDK>/sdk";
const auth: PublicOauthClient = createPublicOauthClient(
"CLIENT_ID", "FOUNDRY_API_URL", "REDIRECT_URL"
);
const client: Client = createClient( "FOUNDRY_API_URL", $ontologyRid, auth);
The following will return all user attributes for the user that is currently logged in:
import { User, Users } from "@osdk/foundry.admin"
const currentUserDetails: User = await Users.getCurrent(client);
thanks for the info when i call await Users.getCurrent(client)
I get a 403 error do you know why that be?
Could you check if the admin read operation is toggled under the Platform SDK tab:
You can find this in the Resources
tab → Platform SDK resources
in your Developer Console app.
Yes i have that turned on now thanks for sharing that info
But I am still getting an error
{
"errorCode": "PERMISSION_DENIED",
"errorName": "ApiUsageDenied",
"errorInstanceId": "aceaebf1-045b-47d0-94c6-eadecd16b50b",
"parameters": {
"missingScope": "api:usage:admin-read"
}
}
I have updated my scopes
import { Client, createClient } from '@osdk/client';
import { $ontologyRid } from '@emma-prototype/sdk';
import { createPublicOauthClient } from '@osdk/oauth';
const url = import.meta.env.VITE_FOUNDRY_API_URL;
const clientId = import.meta.env.VITE_FOUNDRY_CLIENT_ID;
const redirectUrl = import.meta.env.VITE_FOUNDRY_REDIRECT_URL;
const scopes = [
'api:ontologies-read',
'api:ontologies-write',
'api:admin-read',
'api:mediasets-read',
'api:mediasets-write',
];
checkEnv(url, 'VITE_FOUNDRY_API_URL');
checkEnv(clientId, 'VITE_FOUNDRY_CLIENT_ID');
checkEnv(redirectUrl, 'VITE_FOUNDRY_REDIRECT_URL');
function checkEnv(
value: string | undefined,
name: string
): asserts value is string {
if (value == null) {
throw new Error(`Missing environment variable: ${name}`);
}
}
export const auth = createPublicOauthClient(
clientId,
url,
redirectUrl,
true,
undefined,
window.location.toString(),
scopes
);
/**
* Initialize the client to interact with the Ontology SDK
*/
const client: Client = createClient(url, $ontologyRid, auth);
export default client;
I have generated a new osdk and imported it into my code base not sure if I need to do something else.
This may be related to a stale refresh token.
Could you:
- try opening in incognito and see if the problem still persists
- share the version of
@osdk/oauth
that you are currently using
Thank you it looks to be working now.
Glad to hear! Looks like this was due to a stale refresh token that didn’t update when you updated the scopes.
If you bump @osdk/oauth
to version 1.1.1
, that issue should now be resolved for future scope changes.