Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: migrate signup and bytebase login to v2 API (#4106)

Danny Xu487198921dbb
diff --git a/frontend/src/store/modules/auth.ts b/frontend/src/store/modules/auth.ts
index fdddf1be3c..5800c63e8a 100644
--- a/frontend/src/store/modules/auth.ts
+++ b/frontend/src/store/modules/auth.ts
@@ -12,6 +12,7 @@ import {
unknown,
PrincipalId,
AuthProvider,
+ BytebaseLoginInfo,
} from "@/types";
import { getIntCookie } from "@/utils";
import { usePrincipalStore } from "./principal";
@@ -50,15 +51,31 @@ export const useAuthStore = defineStore("auth", {
return convertedProviderList;
},
async login(loginInfo: LoginInfo) {
+ if (loginInfo.authProvider == "BYTEBASE") {
+ await axios.post("/v1/auth/login", {
+ email: (loginInfo.payload as BytebaseLoginInfo).email,
+ password: (loginInfo.payload as BytebaseLoginInfo).password,
+ web: true,
+ });
+ const userId = getIntCookie("user");
+ if (userId) {
+ const loggedInUser = await usePrincipalStore().fetchPrincipalById(
+ userId
+ );
+
+ this.setCurrentUser(loggedInUser);
+ return loggedInUser;
+ }
+ return unknown("PRINCIPAL") as Principal;
+ }
+
const loggedInUser = (
await axios.post(`/api/auth/login/${loginInfo.authProvider}`, {
data: { type: "loginInfo", attributes: loginInfo.payload },
})
).data.data;
-
// Refresh the corresponding principal
await usePrincipalStore().fetchPrincipalById(loggedInUser.id);
-
// The conversion relies on the above refresh.
const convertedUser = convert(loggedInUser);
this.setCurrentUser(convertedUser);
@@ -74,10 +91,16 @@ export const useAuthStore = defineStore("auth", {
return unknownPrincipal;
},
async signup(signupInfo: SignupInfo) {
- await axios.post("/api/auth/signup", {
- data: { type: "signupInfo", attributes: signupInfo },
+ await axios.post("/v1/users", {
+ email: signupInfo.email,
+ title: signupInfo.name,
+ password: signupInfo.password,
+ });
+ await axios.post("/v1/auth/login", {
+ email: signupInfo.email,
+ password: signupInfo.password,
+ web: true,
});
-
const userId = getIntCookie("user");
if (userId) {
const loggedInUser = await usePrincipalStore().fetchPrincipalById(
diff --git a/server/api/auth/header.go b/server/api/auth/header.go
index 2fe81910aa..dbcec99f27 100644
--- a/server/api/auth/header.go
+++ b/server/api/auth/header.go
@@ -16,13 +16,13 @@ func GatewayResponseModifier(ctx context.Context, response http.ResponseWriter,
if !ok {
return errors.Errorf("failed to get ServerMetadata from context in the gateway response modifier")
}
- processMetadata(md, GatewayMetadataAccessTokenKey, AccessTokenCookieName, response)
- processMetadata(md, GatewayMetadataRefreshTokenKey, RefreshTokenCookieName, response)
- processMetadata(md, GatewayMetadataUserIDKey, UserIDCookieName, response)
+ processMetadata(md, GatewayMetadataAccessTokenKey, AccessTokenCookieName, true /* httpOnly */, response)
+ processMetadata(md, GatewayMetadataRefreshTokenKey, RefreshTokenCookieName, true /* httpOnly */, response)
+ processMetadata(md, GatewayMetadataUserIDKey, UserIDCookieName, false /* httpOnly */, response)
return nil
}
-func processMetadata(md runtime.ServerMetadata, metadataKey, cookieName string, response http.ResponseWriter) {
+func processMetadata(md runtime.ServerMetadata, metadataKey, cookieName string, httpOnly bool, response http.ResponseWriter) {
values := md.HeaderMD.Get(metadataKey)
if len(values) == 0 {
return
@@ -31,18 +31,18 @@ func processMetadata(md runtime.ServerMetadata, metadataKey, cookieName string,
if value == "" {
unsetCookie(response, cookieName)
} else {
- setCookie(response, cookieName, value)
+ setCookie(response, cookieName, value, httpOnly)
}
}
-func setCookie(response http.ResponseWriter, key, value string) {
+func setCookie(response http.ResponseWriter, key, value string, httpOnly bool) {
http.SetCookie(response, &http.Cookie{
Name: key,
Value: value,
Expires: time.Now().Add(CookieExpDuration),
Path: "/",
// Http-only helps mitigate the risk of client side script accessing the protected cookie.
- HttpOnly: true,
+ HttpOnly: httpOnly,
// For now, we allow Bytebase to run on non-https host, see https://github.com/bytebase/bytebase/issues/31
// cookie.Secure = true
SameSite: http.SameSiteStrictMode,