Skip to content

Commit

Permalink
Merge pull request #27 from Virus288/feat/accountDelete
Browse files Browse the repository at this point in the history
Feat: Added button to remove user's account
  • Loading branch information
Virus288 authored Dec 3, 2024
2 parents a51fc45 + 58494dd commit 2d29349
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "authorizations-client",
"version": "0.1.0",
"version": "0.3.0",
"description": "AuthorizationsClient",
"productName": "AuthorizationsClient",
"author": "https://github.com/Virus288",
Expand Down
22 changes: 21 additions & 1 deletion src/components/generic/views/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from 'react';
import { HomeBody, HomeRow } from '../styled';
import { Button } from '../../customs/buttons';
import { sendToLoginPage, sendToRegisterPage, sendToLogoutPage, getUserinfo } from '../../../controllers';
import { sendToLoginPage, sendToRegisterPage, sendToLogoutPage, getUserinfo, refreshTokens, removeAccount } from '../../../controllers';

const Home: React.FC = () => {
const [userinfo, setUserinfo] = useState<string | null>(null)
Expand All @@ -15,6 +15,24 @@ const Home: React.FC = () => {
})
}

const handleRefreshToken = () => {
refreshTokens().then(() => {
setUserinfo('Refreshed tokens')
}).catch(err => {
console.log((err as Error).message)
setUserinfo('Got error while refreshing tokens')
})
}

const handleRemoveAccount = () => {
removeAccount().then(() => {
setUserinfo('Account removed')
}).catch(err => {
console.log((err as Error).message)
setUserinfo('Got error while removing account')
})
}

return (
<HomeBody>
<header>
Expand All @@ -27,6 +45,8 @@ const Home: React.FC = () => {
<Button onClick={() => sendToLoginPage()}>Log in</Button>
<Button onClick={() => sendToLogoutPage()}>Log out</Button>
<Button onClick={() => handleGetUserInfo()}>Check if logged in</Button>
<Button onClick={() => handleRefreshToken()}>Refresh access token</Button>
<Button onClick={() => handleRemoveAccount()}>Remove account</Button>
</HomeRow>
</HomeBody>
);
Expand Down
48 changes: 48 additions & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,51 @@ export const getUserinfo = async (): Promise<string> => {
console.log('Got error', await res.json());
return 'None. User is not logged in, or something is wrong';
};

export const refreshTokens = async (): Promise<void> => {
const server = import.meta.env.VITE_API_BACKEND as string;
const homeUrl = import.meta.env.VITE_API_HOME as string;

const res = await fetch(`${server}/user/refresh`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': homeUrl,
},
});

if (res.ok) {
return;
}

console.log('Got error', await res.json());
};

export const removeAccount = async (): Promise<void> => {
const server = import.meta.env.VITE_API_BACKEND as string;
const homeUrl = import.meta.env.VITE_API_HOME as string;
const client = import.meta.env.VITE_API_BACKEND_LOGIN_CLIENT as string;

const query = new URLSearchParams({
client,
});

const res = await fetch(`${server}/user?${query.toString()}`, {
method: 'DELETE',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': homeUrl,
},
});

if (res.ok) {
return;
}

console.log('Got error', await res.json());

const err = (await res.json()) as Error;
throw err;
};

0 comments on commit 2d29349

Please sign in to comment.