diff --git a/src/App.tsx b/src/App.tsx index 5974053..b6baaf4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,7 +2,6 @@ import LoginPage from "pages/LoginPage"; import StorePage from "pages/StorePage"; import React, { useContext, useEffect, FC } from "react"; import { BrowserRouter as Router, Route, Switch } from "react-router-dom"; -import { INVENTORY_URI } from "./api/"; import { StateContext } from "./state/state"; import { setInventory } from "./state/actions"; import { fetchInventory } from "./api/"; @@ -15,7 +14,7 @@ const App: FC = () => { const { user } = state; useEffect(() => { const getData = async () => { - const data = await fetchInventory(INVENTORY_URI); + const data = await fetchInventory(); if (!state.inventory.length) { dispatch(setInventory(data)); diff --git a/src/api/authorization.ts b/src/api/authorization.ts index 5ce4c68..2e710e4 100644 --- a/src/api/authorization.ts +++ b/src/api/authorization.ts @@ -1,27 +1,10 @@ -import { - authorizedGet, - authorizedPost, - LOGIN_URI, - REGISTER_RFID_URI, -} from "api"; +import { LOGIN_URL, REGISTER_URL, get, post } from "api"; import { UserResponse } from "types/api"; import { User } from "types/user"; -import { fetchToken } from "api/token"; - -const getUser = async (url: string): Promise => { - const response = await authorizedGet({ url }); - // No token - if (response.status == 401) { - await fetchToken(); - const retryResponse = await authorizedGet({ url }); - return retryResponse; - } - return response; -}; export const login = async (rfid: string): Promise => { - const url = LOGIN_URI(rfid); - const response = await getUser(url); + const url = LOGIN_URL(localStorage.getItem("AZURE_DEFAULT_TOKEN")!, rfid); + const response = await get({ url }); const json = await response.json(); return json as UserResponse; }; @@ -29,7 +12,6 @@ export const login = async (rfid: string): Promise => { export const handleRfid = async (rfid: string): Promise => { const user = await login(rfid); if (user.count) { - // As it returns a weird response const { pk, saldo, first_name } = user.results[0]; // The first and only user return { pk, balance: saldo, first_name }; } @@ -41,10 +23,11 @@ export const registerUser = ( password: string, rfid: string ): Promise => { + const url = REGISTER_URL(localStorage.getItem("AZURE_DEFAULT_TOKEN")!); const data = { username, password, rfid, }; - return authorizedPost({ url: REGISTER_RFID_URI, body: data }); + return post({ url, body: data }); }; diff --git a/src/api/index.ts b/src/api/index.ts index c65fd54..d293efd 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,21 +1,14 @@ -import { loadToken } from "api/token"; import { Product } from "../types/inventory"; -export const CLIENT_ID = encodeURIComponent( - process.env.REACT_APP_CLIENT_ID || "" -); -export const CLIENT_SECRET = encodeURIComponent( - process.env.REACT_APP_CLIENT_SECRET || "" -); +export const LOGIN_URL = (code: string, rfid: string): string => + `https://nibble4-validator.azurewebsites.net/api/login?code=${code}&&rfid=${rfid}`; +export const FETCH_INVENTORY_URL = (code: string): string => + `https://nibble4-validator.azurewebsites.net/api/fetchInventory?code=${code}`; +export const PURCHASE_URL = (code: string): string => + `https://nibble4-validator.azurewebsites.net/api/purchase?code=${code}`; +export const REGISTER_URL = (code: string): string => + `https://nibble4-validator.azurewebsites.net/api/createUser?code=${code}`; -const API_BASE = process.env.REACT_APP_API_BASE || ""; -export const AUTHORIZE_URI = `${API_BASE}/auth/`; -export const REGISTER_RFID_URI = `${API_BASE}/rfid/`; -export const INVENTORY_URI = `${process.env.REACT_APP_API_BASE}/inventory/`; -export const BALANCE_URI = `${process.env.REACT_APP_API_BASE}/transactions/`; // Update balance -export const TRANSACTION_URI = `${process.env.REACT_APP_API_BASE}/orderline/`; // purchase item -export const LOGIN_URI = (rfid: string): string => - `${API_BASE}/usersaldo/?rfid=${rfid}`; export const IMAGE_URI = (sm: string): string => `https://online.ntnu.no/${sm}`; type AJAXArguments = { @@ -31,21 +24,6 @@ export const get = ({ url, body, headers }: AJAXArguments): Promise => headers: headers, }); -export const authorizedGet = ({ - url, - body, - headers, -}: AJAXArguments): Promise => - get({ - url, - body, - headers: { - ...headers, - Authorization: `Bearer ${loadToken()}`, - "Content-Type": "application/json", - }, - }); - export const post = ({ url, body, @@ -57,22 +35,8 @@ export const post = ({ headers, }); -export const authorizedPost = ({ - url, - body, - headers, -}: AJAXArguments): Promise => - post({ - url, - body, - headers: { - ...headers, - "Content-Type": "application/json", - Authorization: `Bearer ${loadToken()}`, - }, - }); - -export const fetchInventory = async (url: string): Promise => { +export const fetchInventory = async (): Promise => { + const url = FETCH_INVENTORY_URL(localStorage.getItem("AZURE_DEFAULT_TOKEN")!); const response = await get({ url }); if (response.ok) { const json = await response.json(); diff --git a/src/api/order.ts b/src/api/order.ts index 5fe0f4d..22e0163 100644 --- a/src/api/order.ts +++ b/src/api/order.ts @@ -1,4 +1,4 @@ -import { TRANSACTION_URI as url, authorizedPost } from "./index"; +import { PURCHASE_URL, post } from "./index"; import { CartItem } from "../types/inventory"; import { OrderLineFormat } from "../types/api"; @@ -6,11 +6,12 @@ const purchaseItems = ( id: number, orders: { [id: number]: CartItem } ): Promise => { + const url = PURCHASE_URL(localStorage.getItem("AZURE_DEFAULT_TOKEN")!); const body: OrderLineFormat = { user: id, orders: Object.values(orders), }; - return authorizedPost({ url, body }); + return post({ url, body }); }; export default purchaseItems; diff --git a/src/api/token.ts b/src/api/token.ts deleted file mode 100644 index ff4838a..0000000 --- a/src/api/token.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { AUTHORIZE_URI, CLIENT_ID, CLIENT_SECRET, post } from "api"; - -const saveToken = (token: string) => - localStorage.setItem("nibble4_token", token); -export const loadToken = (): string => - localStorage.getItem("nibble4_token") || ""; - -export const fetchToken = async (): Promise => { - // Currently it returns 'unsuported grant_type' when using application/json. Should look into it. - const payload = `grant_type=client_credentials&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}`; - const response = await post({ - url: AUTHORIZE_URI, - body: payload, - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }); - const json = await response.json(); - saveToken(json.access_token as string); -};