Skip to content

Commit

Permalink
token schema parse
Browse files Browse the repository at this point in the history
  • Loading branch information
toto04 committed Oct 15, 2023
1 parent ed15a27 commit 84491e5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/api/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import axios, {
AxiosResponse,
InternalAxiosRequestConfig,
} from "axios"
import { PolimiToken, PoliNetworkToken, Tokens } from "./schemas"
import { PolimiToken, PoliNetworkToken, Tokens, tokensSchema } from "./schemas"
import AsyncStorage from "@react-native-async-storage/async-storage"
import { wait } from "utils/functions"
import { Alert } from "react-native"
Expand Down Expand Up @@ -432,8 +432,7 @@ export class HttpClient extends EventEmitter {
async loadTokens() {
const tokens = await AsyncStorage.getItem("api:tokens")
if (tokens) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const parsedTokens: Tokens = JSON.parse(tokens)
const parsedTokens: Tokens = tokensSchema.parse(JSON.parse(tokens))
console.log("Loaded tokens from local storage")
this.polimiToken = parsedTokens.polimiToken
this.poliNetworkToken = parsedTokens.poliNetworkToken
Expand Down
11 changes: 8 additions & 3 deletions src/pages/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import WebView from "react-native-webview"
import { usePalette } from "utils/colors"
import { api } from "api"
import { HttpClient } from "api/HttpClient"
import { PolimiToken, PoliNetworkToken } from "api/schemas"
import {
PolimiToken,
PoliNetworkToken,
poliNetworkTokenSchema,
} from "api/schemas"
import { NavBar } from "components/NavBar"

// TODO: HANDLE ERRORS, this will break as soon as something goes wrong
Expand Down Expand Up @@ -132,8 +136,9 @@ export const Login: RootStackScreen<"Login"> = () => {

if (url.startsWith(polinetworkTargetUrl)) {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setPoliNetworkToken(JSON.parse(data))
setPoliNetworkToken(
poliNetworkTokenSchema.parse(JSON.parse(data))
)
setCurrentURL(magicTokenUrl)
} catch (e) {
console.log("error while parsing!!")
Expand Down

0 comments on commit 84491e5

Please sign in to comment.