-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc6445e
commit c80f2dd
Showing
9 changed files
with
108 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {setToken, token} from "~/stores/auth"; | ||
|
||
const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API; | ||
|
||
export const api = { | ||
get: async (url: string) => { | ||
const response = await fetch(`${FAUCET_API_URL}/${url}`, { | ||
headers: { | ||
Authorization: `Bearer ${token()}`, | ||
}, | ||
}); | ||
|
||
if (response.status === 401) { | ||
// Handle unauthorized | ||
setToken(null); | ||
localStorage.removeItem("token"); | ||
window.location.href = "/"; | ||
return; | ||
} | ||
|
||
return response.json(); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Component } from "solid-js"; | ||
import { token, login, logout } from "~/stores/auth"; | ||
|
||
export const AuthButton: Component = () => { | ||
return ( | ||
<button | ||
onClick={() => token() ? logout() : login()} | ||
class="px-4 py-2 bg-blue-500 text-white rounded" | ||
> | ||
{token() ? "Logout" : "Login with GitHub"} | ||
</button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {createEffect, createSignal} from "solid-js"; | ||
|
||
// Check if window is defined (client-side) | ||
const isClient = typeof window !== 'undefined'; | ||
|
||
// Create signals for auth state | ||
export const [token, setToken] = createSignal<string | null>( | ||
isClient ? localStorage.getItem("token") : null | ||
); | ||
|
||
|
||
const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API; | ||
|
||
export const login = () => { | ||
window.location.href = `${FAUCET_API_URL}/auth/github`; | ||
}; | ||
|
||
export const logout = () => { | ||
localStorage.removeItem("token"); | ||
setToken(null); | ||
}; |