Skip to content

Commit

Permalink
Changes to token handling
Browse files Browse the repository at this point in the history
  • Loading branch information
belst committed Jan 18, 2022
1 parent c2aded8 commit d64aa6a
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 125 deletions.
2 changes: 1 addition & 1 deletion frontend/.env.development
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VITE_BASEURL="127.0.0.1:8080"
VITE_BASEURL="localhost:8080"
VITE_PROTOCOL="http://"
VITE_APIPATH=""
98 changes: 43 additions & 55 deletions frontend/src/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,65 +1,53 @@
import { Navigate, useNavigate } from "solid-app-router";
import { Component, createMemo, createResource, createSignal, For, Show } from "solid-js";
import { Navigate } from "solid-app-router";
import { Component, createResource, Show } from "solid-js";
import { useService } from "solid-services";
import Layout from "./Layout";
import { AuthService } from "./store/AuthService";
import { IToken } from "./types/user.interface";

const Dashboard: Component = () => {

const authService = useService(AuthService);
const [tokens] = createResource(() => {
return authService().client.common.tokens();
});

const [selectedToken, setSelectedToken] = createSignal<IToken>();
const navigate = useNavigate();


// createMemo(() => {
// if (!authService().isLoggedIn) {
// navigate('/login', { replace: true, state: { redirectTo: '/dashboard' } });
// }
// });

return (
<>
<Show when={authService().user === null}>
<Navigate href="/login" state={{ redirectTo: '/dashboard' }} />
</Show>
<Layout>
<div class="rounded-lg shadow bg-base-200 drawer drawer-mobile h-52">
<input id="my-drawer-2" type="checkbox" class="drawer-toggle" />
<div class="flex flex-col items-center justify-center drawer-content">
<label for="my-drawer-2" class="mb-4 btn btn-primary drawer-button lg:hidden">open menu</label>
<div class="text-xs text-center">
<Show when={selectedToken()} fallback="Please select a token">
{selectedToken().token}
</Show>
</div>
</div>
<div class="drawer-side">
<label for="my-drawer-2" class="drawer-overlay"></label>
<ul class="menu p-4 overflow-y-auto w-80 bg-base-100 text-base-content">
<For each={tokens()}>
{(token) => (
<li>
<a
classList={{ active: token.token === selectedToken()?.token }}
onclick={(_e) => setSelectedToken(token)}
>
{token.name}
</a>
</li>
)}
</For>

</ul>
</div>
const authService = useService(AuthService);
const [options, { refetch }] = createResource(() => {
return authService().client.common.options();
});

const reset = () =>
authService().client.common.reset()
.then(refetch);

return (
<>
<Show when={authService().user === null}>
<Navigate href="/login" state={{ redirectTo: '/dashboard' }} />
</Show>
<Layout>
<div class="rounded-lg shadow bg-base-200 drawer drawer-mobile h-52">
<input id="my-drawer-2" type="checkbox" class="drawer-toggle" />
<div class="flex flex-col items-center justify-center drawer-content">
<label for="my-drawer-2" class="mb-4 btn btn-primary drawer-button lg:hidden">open menu</label>
<div class="text-xs text-center">
<div class="form-control">
<div class="relative">
<input class="input input-bordered w-[50ex]" type="text" readonly value={options()?.token || 'Create Token'} />
<button onclick={reset} class="absolute top-0 right-0 rounded-l-none btn btn-primary">{options() ? 'reset' : 'create'}</button>
</div>
</Layout>
</>
);
</div>
</div>
</div>
<div class="drawer-side">
<label for="my-drawer-2" class="drawer-overlay"></label>
<ul class="menu p-4 overflow-y-auto w-80 bg-base-100 text-base-content">
<li>
<a classList={{ active: true }}>
Stream Token
</a>
</li>
</ul>
</div>
</div>
</Layout>
</>
);
}

export default Dashboard;
2 changes: 1 addition & 1 deletion frontend/src/Stream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ const Stream: Component = () => {
);
};

export default Stream;
export default Stream;
2 changes: 1 addition & 1 deletion frontend/src/store/AuthService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSignal } from 'solid-js';
import { IToken, IUser } from '../types/user.interface';
import { IUser } from '../types/user.interface';
import { ovenAuthClient } from './api';

export function AuthService() {
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/store/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IToken, IUser } from "../types/user.interface";
import { IStreamOption, IUser } from "../types/user.interface";

function httpClient(endpoint: string, request: typeof fetch) {
// let auth = "";
Expand Down Expand Up @@ -69,9 +69,12 @@ export function ovenAuthClient(endpoint: string, request = fetch) {
users(): Promise<IUser[]> {
return client.get('/users')('users');
},
tokens(): Promise<IToken[]> {
return client.get('/tokens')('tokens');
options(): Promise<IStreamOption> {
return client.get('/options')('options');
},
reset(): Promise<void> {
return client.post('/reset')();
}
},

auth: {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/types/user.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export interface IUser {
id: number;
username: string;
hidden: boolean;
}

export interface IToken {
export interface IStreamOption {
token: string;
user_id: number;
name: string;
Expand Down
1 change: 1 addition & 0 deletions migrations/20220118174037_change_tokens_to_options.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table tokens rename to options;
1 change: 1 addition & 0 deletions migrations/20220118174400_add_hidden_to_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table users add column hidden boolean not null default false;
1 change: 1 addition & 0 deletions migrations/20220118201424_make_userid_unique.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table options add unique (user_id);
Loading

0 comments on commit d64aa6a

Please sign in to comment.