Skip to content

Commit

Permalink
feat: support email suffix checkout
Browse files Browse the repository at this point in the history
feat: support email suffix checkout
Co-Authored-By: Minghan Zhang <[email protected]>
  • Loading branch information
Sh1n3zZ and zmh-program committed Apr 4, 2024
1 parent e60b16f commit a888fa6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
9 changes: 5 additions & 4 deletions app/src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ export async function doRegister(
}
}

export async function doVerify(email: string): Promise<VerifyResponse> {
export async function doVerify(email: string, checkout?: boolean): Promise<VerifyResponse> {
try {
const response = await axios.post("/verify", {
email,
email, checkout,
} as VerifyForm);
return response.data as VerifyResponse;
} catch (e) {
Expand All @@ -115,10 +115,11 @@ export async function sendCode(
t: any,
toast: any,
email: string,
checkout?: boolean,
): Promise<boolean> {
if (email.trim().length === 0 || !isEmailValid(email)) return false;

const res = await doVerify(email);
const res = await doVerify(email, checkout);
if (!res.status)
toast({
title: t("auth.send-code-failed"),
Expand All @@ -131,4 +132,4 @@ export async function sendCode(
});

return res.status;
}
}
2 changes: 1 addition & 1 deletion app/src/components/home/assemblies/ChatAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function MarketAction() {

return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger>
<DialogTrigger asChild>
<ChatAction text={t("market.title")}>
<Blocks className={`h-4 w-4`} />
</ChatAction>
Expand Down
2 changes: 1 addition & 1 deletion app/src/routes/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function Verify({ form, dispatch, setNext }: CompProps) {
await router.navigate("/");
};

const onVerify = async () => await sendCode(t, toast, form.email);
const onVerify = async () => await sendCode(t, toast, form.email, true);

return (
<div className={`auth-wrapper`}>
Expand Down
13 changes: 10 additions & 3 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"database/sql"
"errors"
"fmt"
"strings"
"time"

"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/spf13/viper"
"strings"
"time"
)

func ParseToken(c *gin.Context, token string) *User {
Expand Down Expand Up @@ -90,10 +91,16 @@ func generateCode(c *gin.Context, cache *redis.Client, email string) string {
return code
}

func Verify(c *gin.Context, email string) error {
func Verify(c *gin.Context, email string, checkout bool) error {
cache := utils.GetCacheFromContext(c)
code := generateCode(c, cache, email)

if checkout {
if err := channel.SystemInstance.IsValidMail(email); err != nil {
return err
}
}

return channel.SystemInstance.SendVerifyMail(email, code)
}

Expand Down
8 changes: 5 additions & 3 deletions auth/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"chat/channel"
"chat/globals"
"chat/utils"
"github.com/gin-gonic/gin"
"net/http"
"strings"

"github.com/gin-gonic/gin"
)

type RegisterForm struct {
Expand All @@ -17,7 +18,8 @@ type RegisterForm struct {
}

type VerifyForm struct {
Email string `form:"email" binding:"required"`
Email string `form:"email" binding:"required"`
Checkout bool `form:"checkout"`
}

type LoginForm struct {
Expand Down Expand Up @@ -228,7 +230,7 @@ func VerifyAPI(c *gin.Context) {
return
}

if err := Verify(c, form.Email); err != nil {
if err := Verify(c, form.Email, form.Checkout); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": false,
"error": err.Error(),
Expand Down

0 comments on commit a888fa6

Please sign in to comment.