Skip to content

Commit

Permalink
refactor: make Scopes type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
2paperstar committed Jan 8, 2024
1 parent fadfbe9 commit 33cd99c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/api/oauth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Scope } from "src/utils/schema";
import { z } from "zod";
import { Scopes } from "src/utils/schema";

import api from ".";

Expand All @@ -13,7 +12,7 @@ export const getClientInformation = (clientId: string) =>
.then(({ data }) => ({
id: data.id,
name: data.name,
recentConsent: data.recent_consent as z.infer<typeof Scope>[],
recentConsent: data.recent_consent as Scopes,
}));

export const authorize = ({
Expand Down
7 changes: 2 additions & 5 deletions src/pages/Authorize/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import Button from "src/components/Button";
import Logo from "src/components/Logo";
import { Scope } from "src/utils/schema";
import { Scopes } from "src/utils/schema";
import styled from "styled-components";
import { z } from "zod";

import useAuthorize from "./useAuthorize";

Expand Down Expand Up @@ -46,9 +45,7 @@ const Item = styled.li`

const Authorize = () => {
const { error, consent, scopesNotConsented, clientData } = useAuthorize();
const [scopesConsented, setScopesConsented] = useState<
z.infer<typeof Scope>[]
>([]);
const [scopesConsented, setScopesConsented] = useState<Scopes>([]);
const { t } = useTranslation();

useEffect(() => {
Expand Down
12 changes: 4 additions & 8 deletions src/pages/Authorize/useAuthorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
authorizeSchema,
isConsentRequiredScope,
isNotConsentRequiredScope,
Scope,
Scopes,
} from "src/utils/schema";
import useSWR from "swr";
import { z } from "zod";
Expand Down Expand Up @@ -51,14 +51,10 @@ const useAuthorize = () => {
const { user, logout } = useAuth();
const navigate = useNavigate();
const [error, setError] = useState<string>();
const [scopesConsented, setScopesConsented] = useState<
z.infer<typeof Scope>[]
>([]);
const [scopesNotConsented, setScopesNotConsented] = useState<
z.infer<typeof Scope>[]
>([]);
const [scopesConsented, setScopesConsented] = useState<Scopes>([]);
const [scopesNotConsented, setScopesNotConsented] = useState<Scopes>([]);

const consent = (scopes: z.infer<typeof Scope>[]) => {
const consent = (scopes: Scopes) => {
setScopesConsented((prev) => [...prev, ...scopes]);
setScopesNotConsented([]);
};
Expand Down
1 change: 1 addition & 0 deletions src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const Scope = z.enum([
"phone",
"student_id",
]);
export type Scopes = z.infer<typeof Scope>[];
const ScopeRequireConsent = Scope.exclude(["openid", "offline_access"]);
export const isConsentRequiredScope = (scope: string) =>
ScopeRequireConsent.safeParse(scope).success;
Expand Down

0 comments on commit 33cd99c

Please sign in to comment.