Skip to content

Commit

Permalink
Finalize login dialog and add a token service
Browse files Browse the repository at this point in the history
  • Loading branch information
meghanmae committed May 21, 2024
1 parent ff9bcbb commit cb9e764
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
7 changes: 7 additions & 0 deletions wordle-web/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<v-card>
<v-card-text>
<v-btn
v-if="!tokenService.isLoggedIn()"
@click="showLoginDialog = true"
class="mb-5"
flat
Expand All @@ -25,6 +26,9 @@
<v-icon> mdi-lock </v-icon>
Login
</v-btn>
<div v-else>
{{ tokenService.getUserName() }}
</div>
<br />
<v-btn @click="toggleTheme" class="mb-5" flat color="primary">
<v-icon> mdi-theme-light-dark </v-icon>
Expand All @@ -51,12 +55,15 @@
<script setup lang="ts">
import { useTheme } from "vuetify";
import nuxtStorage from "nuxt-storage";
import TokenService from "@/scripts/TokenService";
const router = useRouter();
const theme = useTheme();
const showHelpDialog = ref(false);
const showLoginDialog = ref(false);
const tokenService = new TokenService();
onMounted(() => {
var defaultTheme = nuxtStorage.localStorage.getData("theme");
theme.global.name.value = defaultTheme ?? "dark";
Expand Down
6 changes: 5 additions & 1 deletion wordle-web/components/SigninDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

<script setup lang="ts">
import axios from "axios";
import TokenService from "@/scripts/TokenService";
const tokenService = new TokenService();
const modelValue = defineModel<boolean>({ default: false });
const showPassword = ref(false);
Expand All @@ -38,13 +41,14 @@ const password = ref("");
const errorMessage = ref("");
function signIn() {
errorMessage.value = "";
errorMessage.value = "";
axios
.post("/Token/GetToken", {
username: userName.value,
password: password.value,
})
.then((response) => {
tokenService.setToken(response.data.token);
modelValue.value = false;
})
.catch((error) => {
Expand Down
25 changes: 25 additions & 0 deletions wordle-web/scripts/tokenService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default class TokenService {
private tokenKey: string = "token";

public setToken(token: string) {
localStorage.setItem(this.tokenKey, token);
}

public getToken(): string {
return localStorage.getItem(this.tokenKey) ?? "";
}

public isLoggedIn(): boolean {
// Won't work if the token is expired
return this.getToken() !== "";
}

public getUserName() {
const token = this.getToken();
if (token === "") {
return "";
}
console.log(JSON.parse(atob(token.split(".")[1])));
return JSON.parse(atob(token.split(".")[1])).userName;
}
}

0 comments on commit cb9e764

Please sign in to comment.