-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mbaraa/feat/impl-login
Feat: Implement login/signup
- Loading branch information
Showing
27 changed files
with
947 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,8 @@ DB_NAME="" | |
DB_HOST="" | ||
DB_USERNAME="" | ||
DB_PASSWORD="" | ||
|
||
SMTP_HOST="" | ||
SMTP_PORT="" | ||
SMTP_USER="" | ||
SMTP_PASSWORD="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package entities | ||
|
||
type LoginRequest struct { | ||
Email string `json:"email"` | ||
} | ||
|
||
type SignupRequest struct { | ||
Name string `json:"name"` | ||
Email string `json:"email"` | ||
} | ||
|
||
type OtpRequest struct { | ||
Code string `json:"code"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package apis | ||
|
||
import ( | ||
"context" | ||
"dankmuzikk/config" | ||
"dankmuzikk/entities" | ||
"dankmuzikk/handlers" | ||
"dankmuzikk/log" | ||
"dankmuzikk/services/login" | ||
"dankmuzikk/views/components/otp" | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type emailLoginApi struct { | ||
service *login.EmailLoginService | ||
} | ||
|
||
func NewEmailLoginApi(service *login.EmailLoginService) *emailLoginApi { | ||
return &emailLoginApi{service} | ||
} | ||
|
||
func (e *emailLoginApi) HandleEmailLogin(w http.ResponseWriter, r *http.Request) { | ||
var reqBody entities.LoginRequest | ||
err := json.NewDecoder(r.Body).Decode(&reqBody) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
verificationToken, err := e.service.Login(reqBody) | ||
if err != nil { | ||
log.Errorf("[EMAIL LOGIN API]: Failed to login user: %+v, error: %s\n", reqBody, err.Error()) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
http.SetCookie(w, &http.Cookie{ | ||
Name: handlers.VerificationTokenKey, | ||
Value: verificationToken, | ||
HttpOnly: true, | ||
Path: "/api/verify-otp", | ||
Domain: config.Env().Hostname, | ||
Expires: time.Now().UTC().Add(time.Hour / 2), | ||
}) | ||
otp.VerifyOtp().Render(context.Background(), w) | ||
} | ||
|
||
func (e *emailLoginApi) HandleEmailSignup(w http.ResponseWriter, r *http.Request) { | ||
var reqBody entities.SignupRequest | ||
err := json.NewDecoder(r.Body).Decode(&reqBody) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
verificationToken, err := e.service.Signup(reqBody) | ||
if err != nil { | ||
log.Errorf("[EMAIL LOGIN API]: Failed to sign up a new user: %+v, error: %s\n", reqBody, err.Error()) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
http.SetCookie(w, &http.Cookie{ | ||
Name: handlers.VerificationTokenKey, | ||
Value: verificationToken, | ||
HttpOnly: true, | ||
Path: "/api/verify-otp", | ||
Domain: config.Env().Hostname, | ||
Expires: time.Now().UTC().Add(time.Hour / 2), | ||
}) | ||
otp.VerifyOtp().Render(context.Background(), w) | ||
} | ||
|
||
func (e *emailLoginApi) HandleEmailOTPVerification(w http.ResponseWriter, r *http.Request) { | ||
verificationToken, err := r.Cookie(handlers.VerificationTokenKey) | ||
if err != nil { | ||
w.Write([]byte("Invalid verification token")) | ||
return | ||
} | ||
if verificationToken.Expires.After(time.Now().UTC()) { | ||
w.Write([]byte("Expired verification token")) | ||
return | ||
} | ||
|
||
var reqBody entities.OtpRequest | ||
err = json.NewDecoder(r.Body).Decode(&reqBody) | ||
if err != nil { | ||
log.Error(err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
sessionToken, err := e.service.VerifyOtp(verificationToken.Value, reqBody) | ||
// TODO: specify errors further suka | ||
if err != nil { | ||
log.Error(err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
http.SetCookie(w, &http.Cookie{ | ||
Name: handlers.SessionTokenKey, | ||
Value: sessionToken, | ||
HttpOnly: true, | ||
Path: "/", | ||
Domain: config.Env().Hostname, | ||
Expires: time.Now().UTC().Add(time.Hour * 24 * 30), | ||
}) | ||
|
||
w.Header().Set("HX-Redirect", "/") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package handlers | ||
|
||
const ( | ||
VerificationTokenKey = "verification-token" | ||
SessionTokenKey = "token" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type EmailVerificationCode struct { | ||
Id uint `gorm:"primaryKey;autoIncrement"` | ||
AccountId uint | ||
Account Account | ||
Code string `gorm:"not null"` | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
} | ||
|
||
func (e EmailVerificationCode) GetId() uint { | ||
return e.Id | ||
} |
Oops, something went wrong.