Skip to content

Commit

Permalink
Merge pull request #32 from markgcera/Helper-methods-for-authentication
Browse files Browse the repository at this point in the history
Helper methods for authentication
  • Loading branch information
markgcera authored Mar 12, 2024
2 parents bd60e55 + 01db278 commit 941abe4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ This template provides a minimal setup to get React working in Vite with HMR and
## Setup Guide

This project was set up with NodeJS v20.11.0 LTS with yarn.

1. Change directory to
```shell
cd atlas-3-frontend
```
2. Install packages
1. Install packages
```shell
yarn
```
3. Create a file `.env.development.local` in root folder with the following information
2. Create a file `.env.development.local` in root folder with the following information
```text
VITE_IGNORE_MSW=false
```
If you would like to ignore mock service worker, set it to `true`.
3. Create another file `.env.test.local` in root folder with same content as above
4. Run the development server
```shell
yarn dev
Expand Down
12 changes: 6 additions & 6 deletions src/api/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import resolveURL, { apiURL } from "./fetch";
import resolveURL from "./fetch";

const BASE_URL = resolveURL(apiURL);
const LOGIN_URL = `${BASE_URL}/auth/login`;
const LOGOUT_URL = `${BASE_URL}/auth/logout`;
const LOGIN_URL = resolveURL('/auth/login');
const LOGOUT_URL = resolveURL('/auth/logout');
const GET_CSRF_URL = resolveURL('/auth/csrf');

export const signIn = async (username: string, password: string): Promise<any> => {
export const signIn = async (username: string, password: string) => {
const response = await fetch(LOGIN_URL, {
method: 'POST',
headers: {
Expand Down Expand Up @@ -45,7 +45,7 @@ export const signOut = async (): Promise<Response> => {
};

export const getCSRF = async (): Promise<Response> => {
const response = await fetch(BASE_URL, {
const response = await fetch(GET_CSRF_URL, {
method: "POST",
credentials: "include",
headers: {
Expand Down
1 change: 1 addition & 0 deletions src/api/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

const setApiURL = () => {
if (import.meta.env.DEV) {
return import.meta.env.VITE_IGNORE_MSW.toLowerCase() === "true" ? "http://localhost:8000" : "http://localhost:5173"
Expand Down
5 changes: 2 additions & 3 deletions tests/authentication/auth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ describe("authentication helper functions", () => {

test("should get csrf token", async () => {
const response: Response = await getCSRF()
expect(response.headers).toBe({
'Set-Cookie': mockCSRFToken
})
expect(response.headers.has('Set-Cookie'))
expect(response.headers.get('Set-Cookie')).toBe(mockCSRFToken)
})

test("should be able to sign in", async () => {
Expand Down

0 comments on commit 941abe4

Please sign in to comment.