Skip to content

Commit

Permalink
feat: implement login with sso
Browse files Browse the repository at this point in the history
  • Loading branch information
newarifrh committed Aug 30, 2024
1 parent 6f10226 commit 13fd66d
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 52 deletions.
47 changes: 32 additions & 15 deletions src/api/authApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,39 @@ import { useAuthStore } from "@/stores/auth";
import { BASE_URL } from "./api";

export const login = async (payload: any) => {
const auth = useAuthStore();
const auth = useAuthStore();

const response = await fetch(`${BASE_URL}/v1/auth/login`, {
method: "POST",
headers: {
"Authorization": `Bearer ${auth.token}`,
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const response = await fetch(`${BASE_URL}/v1/auth/login`, {
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});

const result = await response.json();
const result = await response.json();

if (!response.ok) {
throw new Error(result.message)
}
if (!response.ok) {
throw new Error(result.message);
}

return result;
};
return result;
};

export const loginSso = async (token: string) => {
const response = await fetch(`${BASE_URL}/v1/users/me`, {
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
});

const result = await response.json();

if (!response.ok) {
throw new Error(result.message);
}

return result;
};
82 changes: 48 additions & 34 deletions src/stores/auth.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
import { login } from '@/api/authApi'
import { defineStore } from 'pinia'
import { login, loginSso } from "@/api/authApi";
import { defineStore } from "pinia";

export const useAuthStore = defineStore({
id: 'auth',
state: () => {
return {
token: '',
isLogin: false,
}
id: "auth",
state: () => {
return {
token: "",
isLogin: false,
};
},
actions: {
logout() {
this.$patch({
token: "",
isLogin: false,
});

this.router.push({ name: "login" });
},
actions: {
logout() {
this.$patch({
token: '',
isLogin: false,
});

this.router.push({ "name": "login" })
},

async login(payload: any) {
const result = await login(payload);

if (result.data) {
this.$patch({
token: result.data.token,
isLogin: true,
});

this.router.push({ "name": "dashboard" })
}

return result;
}

async login(payload: any) {
const result = await login(payload);

if (result.data) {
this.$patch({
token: result.data.token,
isLogin: true,
});

this.router.push({ name: "dashboard" });
}

return result;
},
persist: true,
})

async loginSso(token: any) {
const result = await loginSso(token);

if (result.data) {
this.$patch({
token: token,
isLogin: true,
});

this.router.push({ name: "dashboard" });
}

return result;
},
},
persist: true,
});
54 changes: 51 additions & 3 deletions src/views/LoginView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<span style="margin: 0 10px">OR</span>
<el-divider style="margin-left: 10px"></el-divider>
</div>
<el-button size="large" style="margin-top: 18px; width: 100%" @click="login(formRef)">
<el-button size="large" style="margin-top: 18px; width: 100%" @click="loginSso">
<IconBPS style="width: 1.9em; height: 1.9em; margin-right: 10px" />
Login with SSO BPS
</el-button>
Expand All @@ -43,6 +43,8 @@ import { ref, reactive, watch } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { useAuthStore } from "@/stores/auth";
import { ElNotification } from "element-plus";
import { BASE_URL } from "@/api/api";
import { useRoute } from "vue-router";
const app = import.meta.env.VITE_APP_TITLE;
Expand All @@ -51,7 +53,7 @@ const initialState = {
password: "",
};
const enableSso = ref(false);
const enableSso = ref(true);
const rules = reactive<FormRules<any>>({
email: [
Expand All @@ -72,7 +74,7 @@ const rules = reactive<FormRules<any>>({
const formRef = ref<FormInstance>();
const form = reactive({ ...initialState });
const route = useRoute();
const auth = useAuthStore();
const loading = ref(false);
const error = ref("");
Expand Down Expand Up @@ -115,6 +117,52 @@ const login = async (formEl: FormInstance | undefined) => {
});
};
const loginSso = async () => {
window.location.href = `${BASE_URL}/v1/auth/sso`;
}
const handleError = (error: any) => {
let message = "";
if (error == "user_not_found") {
message = "User tidak ditemukan";
} else {
message = "Terjadi kesalahan, silahkan coba lagi";
}
feedback.value = {
title: "Error",
message: message,
type: "error",
};
};
watch(() => route.query.token, async (token) => {
if (token) {
try {
await auth.loginSso(token);
feedback.value = {
title: "Success",
message: "Successfully logged in",
type: "success",
};
} catch (e) {
if (e instanceof Error) {
feedback.value = {
title: "Error",
message: "Something went wrong, please try again",
type: "error",
};
}
}
}
}, { immediate: true });
watch(() => route.query.error, async (error) => {
if (error) handleError(error);
}, { immediate: true });
watch(
() => feedback.value,
(feedback: any) => {
Expand Down

0 comments on commit 13fd66d

Please sign in to comment.