Skip to content

Commit

Permalink
fix: add prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
peintnermax committed Jan 26, 2024
1 parent d1d999f commit b797368
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
1 change: 1 addition & 0 deletions lib/dist/zitadelAuth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface ZitadelConfig {
post_logout_redirect_uri?: string;
scope?: string;
project_resource_id?: string;
prompt?: string;
}
interface ZitadelAuth {
authorize(): Promise<void>;
Expand Down
5 changes: 3 additions & 2 deletions lib/dist/zitadelAuth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UserManager, WebStorageStateStore, } from "oidc-client-ts";
export function createZitadelAuth(zitadelConfig) {
var _a, _b, _c;
var _a, _b, _c, _d;
const authConfig = {
authority: `${zitadelConfig.issuer}`,
client_id: `${zitadelConfig.client_id}`,
Expand All @@ -9,7 +9,8 @@ export function createZitadelAuth(zitadelConfig) {
scope: (_b = zitadelConfig.scope) !== null && _b !== void 0 ? _b : `openid profile email ${zitadelConfig.project_resource_id
? `urn:zitadel:iam:org:project:id:${zitadelConfig.project_resource_id}:aud urn:zitadel:iam:org:projects:roles`
: ""}`,
post_logout_redirect_uri: `${(_c = zitadelConfig.post_logout_redirect_uri) !== null && _c !== void 0 ? _c : "http://localhost:3000/"}`,
prompt: (_c = zitadelConfig.prompt) !== null && _c !== void 0 ? _c : "",
post_logout_redirect_uri: `${(_d = zitadelConfig.post_logout_redirect_uri) !== null && _d !== void 0 ? _d : "http://localhost:3000/"}`,
response_mode: "query",
};
const userManager = new UserManager({
Expand Down
3 changes: 2 additions & 1 deletion lib/src/zitadelAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
UserManagerSettings,
Logger,
UserManager,
WebStorageStateStore,
} from "oidc-client-ts";
Expand All @@ -12,6 +11,7 @@ export interface ZitadelConfig {
post_logout_redirect_uri?: string;
scope?: string;
project_resource_id?: string;
prompt?: string;
}

interface ZitadelAuth {
Expand All @@ -35,6 +35,7 @@ export function createZitadelAuth(zitadelConfig: ZitadelConfig): ZitadelAuth {
? `urn:zitadel:iam:org:project:id:${zitadelConfig.project_resource_id}:aud urn:zitadel:iam:org:projects:roles`
: ""
}`,
prompt: zitadelConfig.prompt ?? "",
post_logout_redirect_uri: `${
zitadelConfig.post_logout_redirect_uri ?? "http://localhost:3000/"
}`,
Expand Down
5 changes: 3 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import Callback from "./components/Callback";

function App() {
const config: ZitadelConfig = {
issuer: "https://maxsecond-ekvdou.zitadel.cloud/",
client_id: "251008059999082377@react",
issuer: "",
client_id: "",
prompt: "login",
};

const zitadel = createZitadelAuth(config);
Expand Down
53 changes: 26 additions & 27 deletions src/components/Callback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Props = {
setUserInfo: any;
handleLogout: any;
};

const Callback = ({
issuer,
authenticated,
Expand All @@ -18,24 +19,27 @@ const Callback = ({
setUserInfo,
handleLogout,
}: Props) => {
function loadUserDiscovery(accessToken: string) {
const userInfoEndpoint = `${issuer}oidc/v1/userinfo`;
fetch(userInfoEndpoint, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
.then((response) => response.json())
.then((userInfo) => {
setUserInfo(userInfo);
});
}

useEffect(() => {
if (authenticated === null) {
userManager
.signinRedirectCallback()
.then((user: any) => {
if (user) {
setAuth(true);
const access_token = user.access_token;
const userInfoEndpoint = `${issuer}oidc/v1/userinfo`;
fetch(userInfoEndpoint, {
headers: {
Authorization: `Bearer ${access_token}`,
},
})
.then((response) => response.json())
.then((userInfo) => {
setUserInfo(userInfo);
});
loadUserDiscovery(user.access_token);
} else {
setAuth(false);
}
Expand All @@ -50,17 +54,7 @@ const Callback = ({
.then((user: any) => {
if (user) {
setAuth(true);
const access_token = user.access_token;
const userInfoEndpoint = `${issuer}oidc/v1/userinfo`;
fetch(userInfoEndpoint, {
headers: {
Authorization: `Bearer ${access_token}`,
},
})
.then((response) => response.json())
.then((userInfo) => {
setUserInfo(userInfo);
});
loadUserDiscovery(user.access_token);
} else {
setAuth(false);
}
Expand All @@ -73,12 +67,17 @@ const Callback = ({
if (authenticated === true && userInfo) {
return (
<div className="user">
<h1>Welcome, {userInfo.name}!</h1>
<h2>Welcome, {userInfo.name}!</h2>
<p className="description">Your ZITADEL Profile Information</p>
<h3>Name: {userInfo.name}</h3>
<h3>Email: {userInfo.email}</h3>
<h3>Email Verified: {userInfo.email_verified ? "Yes" : "No"}</h3>
<h3>Locale: {userInfo.locale}</h3>
<p>Name: {userInfo.name}</p>
<p>Email: {userInfo.email}</p>
<p>Email Verified: {userInfo.email_verified ? "Yes" : "No"}</p>
<p>
Roles:{" "}
{JSON.stringify(
userInfo["urn:zitadel:iam:org:project:251288942656156695:roles"]
)}
</p>

<button onClick={handleLogout}>Log out</button>
</div>
Expand Down

0 comments on commit b797368

Please sign in to comment.