Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: refactor login (#13814)
ecmadao11ec572088de
diff --git a/backend/api/gitops/common.go b/backend/api/gitops/common.goindex b091065ab4..20f9ea2705 100644--- a/backend/api/gitops/common.go+++ b/backend/api/gitops/common.go@@ -117,7 +117,7 @@ const ()func getPullRequestComment(externalURL, issue string) string {- return fmt.Sprintf("This pull request has triggered a Bytebase rollout 🚀. Check out the status at %s/%s.", externalURL, issue)+ return fmt.Sprintf("This pull request has triggered a Bytebase rollout 🚀. Check out the status at %s/%s", externalURL, issue)}func convertFileContentToUTF8String(content string) string {diff --git a/frontend/src/components/PasswordSigninForm.vue b/frontend/src/components/PasswordSigninForm.vueindex 4fa104daf8..58c2455318 100644--- a/frontend/src/components/PasswordSigninForm.vue+++ b/frontend/src/components/PasswordSigninForm.vue@@ -86,13 +86,11 @@<script setup lang="ts">import { EyeIcon, EyeOffIcon } from "lucide-vue-next";import { NButton } from "naive-ui";-import { computed, reactive } from "vue";-import { useRoute, useRouter } from "vue-router";+import { storeToRefs } from "pinia";+import { computed, reactive, onMounted } from "vue";+import { useRoute } from "vue-router";import { BBTextField } from "@/bbkit";-import { AUTH_MFA_MODULE, AUTH_PASSWORD_RESET_MODULE } from "@/router/auth";-import { SQL_EDITOR_HOME_MODULE } from "@/router/sqlEditor";-import { useAppFeature, useAuthStore, useSettingV1Store } from "@/store";-import { DatabaseChangeMode } from "@/types/proto/v1/setting_service";+import { useAuthStore, useActuatorV1Store } from "@/store";interface LocalState {email: string;@@ -103,30 +101,43 @@ interface LocalState {const props = withDefaults(defineProps<{- email?: string;- password?: string;- showPassword?: boolean;showForgotPassword?: boolean;}>(),{- email: "",- password: "",- showPassword: false,showForgotPassword: true,});-const router = useRouter();const route = useRoute();const authStore = useAuthStore();+const { isDemo } = storeToRefs(useActuatorV1Store());const state = reactive<LocalState>({- email: props.email,- password: props.password,- showPassword: props.showPassword,+ email: "",+ password: "",+ showPassword: false,isLoading: false,});+onMounted(async () => {+ const url = new URL(window.location.href);+ const params = new URLSearchParams(url.search);+ state.email = params.get("email") ?? (isDemo.value ? "[email protected]" : "");+ state.password = params.get("password") ?? (isDemo.value ? "1024" : "");+ state.showPassword = !!isDemo.value;++ // Try to signin with example account in demo site.+ if (+ (window.location.href.startsWith("https://demo.bytebase.com") ||+ window.location.href.startsWith("https://sql-editor.com")) &&+ isDemo.value &&+ state.email &&+ state.password+ ) {+ await trySignin();+ }+});+const allowSignin = computed(() => {return state.email && state.password;});@@ -135,43 +146,11 @@ const trySignin = async () => {if (state.isLoading) return;state.isLoading = true;try {- const { mfaTempToken, requireResetPassword } = await authStore.login({+ await authStore.login({email: state.email,password: state.password,web: true,});- if (mfaTempToken) {- router.push({- name: AUTH_MFA_MODULE,- query: {- mfaTempToken,- redirect: route.query.redirect as string,- },- });- return;- }- if (requireResetPassword) {- router.push({- name: AUTH_PASSWORD_RESET_MODULE,- });- return;- }- try {- await useSettingV1Store().fetchSettingByName(- "bb.workspace.profile",- /* silent */ true- );- const mode = useAppFeature("bb.feature.database-change-mode");- if (mode.value === DatabaseChangeMode.EDITOR) {- router.replace({- name: SQL_EDITOR_HOME_MODULE,- });- } else {- router.replace("/");- }- } catch {- router.replace("/");- }} finally {state.isLoading = false;}diff --git a/frontend/src/store/modules/auth.ts b/frontend/src/store/modules/auth.tsindex 7cc15fd653..dc54cd2567 100644--- a/frontend/src/store/modules/auth.ts+++ b/frontend/src/store/modules/auth.ts@@ -5,13 +5,19 @@ import { computed, ref } from "vue";import { restartAppRoot } from "@/AppRootContext";import { authServiceClient } from "@/grpcweb";import { router } from "@/router";-import { AUTH_SIGNIN_MODULE } from "@/router/auth";+import {+ AUTH_SIGNIN_MODULE,+ AUTH_PASSWORD_RESET_MODULE,+ AUTH_MFA_MODULE,+} from "@/router/auth";+import { SQL_EDITOR_HOME_MODULE } from "@/router/sqlEditor";+import { useAppFeature, useUserStore, useSettingV1Store } from "@/store";import { unknownUser } from "@/types";import type { LoginRequest, User } from "@/types/proto/v1/auth_service";import { LoginResponse } from "@/types/proto/v1/auth_service";import { UserType } from "@/types/proto/v1/auth_service";+import { DatabaseChangeMode } from "@/types/proto/v1/setting_service";import { getIntCookie } from "@/utils";-import { useUserStore } from ".";export const useAuthStore = defineStore("auth_v1", () => {const userStore = useUserStore();@@ -52,18 +58,55 @@ export const useAuthStore = defineStore("auth_v1", () => {}};+ const getRedirectQuery = () => {+ const query = new URLSearchParams(window.location.search);+ return query.get("redirect");+ };+const login = async (- request: Partial<LoginRequest>- ): Promise<LoginResponse> => {+ request: Partial<LoginRequest>,+ redirect: string = ""+ ) => {const { data } = await axios.post<LoginResponse>("/v1/auth/login", request);++ const redirectUrl = redirect || getRedirectQuery();if (data.mfaTempToken) {- return data;+ return router.push({+ name: AUTH_MFA_MODULE,+ query: {+ mfaTempToken: data.mfaTempToken,+ redirect: redirectUrl,+ },+ });}await restoreUser();setRequireResetPassword(data.requireResetPassword);- return data;+ await useSettingV1Store().getOrFetchSettingByName(+ "bb.workspace.profile",+ /* silent */ true+ );+ const mode = useAppFeature("bb.feature.database-change-mode");++ let nextPage = redirectUrl || "/";+ if (mode.value === DatabaseChangeMode.EDITOR) {+ const route = router.resolve({+ name: SQL_EDITOR_HOME_MODULE,+ });+ nextPage = route.fullPath;+ }++ if (data.requireResetPassword) {+ return router.push({+ name: AUTH_PASSWORD_RESET_MODULE,+ query: {+ redirect: nextPage,+ },+ });+ }++ return router.replace(nextPage);};const signup = async (request: Partial<User>) => {@@ -91,15 +134,14 @@ export const useAuthStore = defineStore("auth_v1", () => {currentUserId.value = undefined;restartAppRoot();- const query = new URLSearchParams(window.location.search);- const redirect = query.get("redirect");const pathname = location.pathname;router.push({name: AUTH_SIGNIN_MODULE,query: {redirect:- redirect || (pathname.startsWith("/auth") ? undefined : pathname),+ getRedirectQuery() ||+ (pathname.startsWith("/auth") ? undefined : pathname),},}).then(() => {diff --git a/frontend/src/views/OAuthCallback.vue b/frontend/src/views/OAuthCallback.vueindex ac17d58d9b..597ca9df77 100644--- a/frontend/src/views/OAuthCallback.vue+++ b/frontend/src/views/OAuthCallback.vue@@ -16,8 +16,8 @@import { NButton } from "naive-ui";import { parse } from "qs";import { onMounted, reactive } from "vue";-import { useRoute, useRouter } from "vue-router";-import { AUTH_MFA_MODULE, AUTH_SIGNIN_MODULE } from "@/router/auth";+import { useRoute } from "vue-router";+import { AUTH_SIGNIN_MODULE } from "@/router/auth";import { useAuthStore } from "@/store";import type { OAuthState, OAuthWindowEventPayload } from "../types";@@ -28,7 +28,6 @@ interface LocalState {payload: OAuthWindowEventPayload;}-const router = useRouter();const route = useRoute();const authStore = useAuthStore();@@ -86,28 +85,18 @@ const triggerAuthCallback = async () => {);window.close();} else {- // We don't need to check password reset for SSO login.- const { mfaTempToken } = await authStore.login({- idpName: eventName.split(".").pop()!,- idpContext: {- oauth2Context: {- code: state.payload.code,+ await authStore.login(+ {+ idpName: eventName.split(".").pop()!,+ idpContext: {+ oauth2Context: {+ code: state.payload.code,+ },},+ web: true,},- web: true,- });- if (mfaTempToken) {- const route = router.resolve({- name: AUTH_MFA_MODULE,- query: {- mfaTempToken,- redirect: oAuthState.redirect || "",- },- });- window.location.href = route.href;- } else {- window.location.href = oAuthState.redirect || "/";- }+ oAuthState.redirect+ );}} else if (eventName.startsWith("bb.oauth.register-vcs") ||diff --git a/frontend/src/views/auth/MultiFactor.vue b/frontend/src/views/auth/MultiFactor.vueindex 28ea24c240..92e5e71166 100644--- a/frontend/src/views/auth/MultiFactor.vue+++ b/frontend/src/views/auth/MultiFactor.vue@@ -68,9 +68,8 @@import { NButton } from "naive-ui";import { computed, reactive } from "vue";import { useI18n } from "vue-i18n";-import { useRoute, useRouter } from "vue-router";+import { useRoute } from "vue-router";import { BBTextField } from "@/bbkit";-import { AUTH_PASSWORD_RESET_MODULE } from "@/router/auth";import { useAuthStore } from "@/store";type MFAType = "OTP" | "RECOVERY_CODE";@@ -83,7 +82,6 @@ interface LocalState {const { t } = useI18n();const route = useRoute();-const router = useRouter();const authStore = useAuthStore();const state = reactive<LocalState>({selectedMFAType: "OTP",@@ -94,9 +92,6 @@ const state = reactive<LocalState>({const mfaTempToken = computed(() => {return route.query.mfaTempToken as string;});-const redirectUrl = computed(() => {- return route.query.redirect as string;-});const challengeDescription = computed(() => {if (state.selectedMFAType === "OTP") {@@ -115,18 +110,10 @@ const challenge = async () => {} else if (state.selectedMFAType === "RECOVERY_CODE") {mfaContext.recoveryCode = state.recoveryCode;}- const { requireResetPassword } = await authStore.login({+ await authStore.login({web: true,mfaTempToken: mfaTempToken.value,...mfaContext,});- // Check if the user needs to reset their password after MFA passed.- if (requireResetPassword) {- router.push({- name: AUTH_PASSWORD_RESET_MODULE,- });- return;- }- router.replace(redirectUrl.value || "/");};</script>diff --git a/frontend/src/views/auth/PasswordReset.vue b/frontend/src/views/auth/PasswordReset.vueindex 7c008d3978..444f73d76b 100644--- a/frontend/src/views/auth/PasswordReset.vue+++ b/frontend/src/views/auth/PasswordReset.vue@@ -67,9 +67,14 @@ const passwordRestrictionSetting = computed(() => settingV1Store.passwordRestriction);+const redirectQuery = computed(() => {+ const query = new URLSearchParams(window.location.search);+ return query.get("redirect") || "/";+});+onMounted(async () => {if (!authStore.requireResetPassword) {- router.replace("/");+ router.replace(redirectQuery.value);return;}await settingV1Store.getOrFetchSettingByName(@@ -104,6 +109,6 @@ const onConfirm = async () => {title: t("common.updated"),});authStore.setRequireResetPassword(false);- router.replace("/");+ router.replace(redirectQuery.value);};</script>diff --git a/frontend/src/views/auth/Signin.vue b/frontend/src/views/auth/Signin.vueindex 21f0c61b2e..b269cb2b5b 100644--- a/frontend/src/views/auth/Signin.vue+++ b/frontend/src/views/auth/Signin.vue@@ -17,11 +17,7 @@name="standard"tab="Standard">- <PasswordSigninForm- :email="state.email"- :password="state.password"- :show-password="state.showPassword"- />+ <PasswordSigninForm /><div class="mt-3"><div@@ -121,7 +117,7 @@attr-type="submit"type="primary"size="large"- :disabled="!allowSignin(identityProvider.name)"+ :disabled="!allowIdPSignin":loading="state.isLoading"style="width: 100%">@@ -184,7 +180,7 @@ import { useRoute, useRouter } from "vue-router";import { BBSpin, BBTextField } from "@/bbkit";import BytebaseLogo from "@/components/BytebaseLogo.vue";import PasswordSigninForm from "@/components/PasswordSigninForm.vue";-import { AUTH_MFA_MODULE, AUTH_SIGNUP_MODULE } from "@/router/auth";+import { AUTH_SIGNUP_MODULE } from "@/router/auth";import {useActuatorV1Store,useAuthStore,@@ -193,7 +189,7 @@ import {import { idpNamePrefix } from "@/store/modules/v1/common";import type { IdentityProvider } from "@/types/proto/v1/idp_service";import { IdentityProviderType } from "@/types/proto/v1/idp_service";-import { isValidEmail, openWindowForSSO } from "@/utils";+import { openWindowForSSO } from "@/utils";import AuthFooter from "./AuthFooter.vue";interface LocalState {@@ -247,12 +243,6 @@ watchEffect(() => {});onMounted(async () => {- const url = new URL(window.location.href);- const params = new URLSearchParams(url.search);- state.email = params.get("email") ?? (isDemo.value ? "[email protected]" : "");- state.password = params.get("password") ?? (isDemo.value ? "1024" : "");- state.showPassword = !!isDemo.value;-await identityProviderStore.fetchIdentityProviderList();// Check if there is an identity provider in the query string and try to sign in with it.@@ -267,49 +257,24 @@ onMounted(async () => {return;}}- // Try to signin with example account in demo site.- if (- (window.location.href.startsWith("https://demo.bytebase.com") ||- window.location.href.startsWith("https://sql-editor.com")) &&- isDemo.value &&- state.email &&- state.password- ) {- await trySignin();- }initialized.value = true;});-const allowSignin = (idpName?: string) => {- if (!idpName) {- return isValidEmail(state.email) && state.password;- }+const allowIdPSignin = computed(() => {return state.email && state.password;-};+});-// Mainly for LDAP signin and demo signin.-const trySignin = async (idpName?: string) => {+// Mainly for LDAP signin.+const trySignin = async (idpName: string) => {if (state.isLoading) return;state.isLoading = true;try {- // For LDAP signin, we don't need to check requireResetPassword.- const { mfaTempToken } = await authStore.login({+ await authStore.login({email: state.email,password: state.password,web: true,idpName: idpName,});- if (mfaTempToken) {- router.push({- name: AUTH_MFA_MODULE,- query: {- mfaTempToken,- redirect: route.query.redirect as string,- },- });- return;- }- router.push("/");} finally {state.isLoading = false;}