tbs
This commit is contained in:
@@ -42,6 +42,7 @@ func NewUser(user *db.User) *User {
|
||||
type Session struct {
|
||||
Id string
|
||||
CreatedAt time.Time
|
||||
ExpiresAt time.Time
|
||||
User *User
|
||||
}
|
||||
|
||||
@@ -49,6 +50,7 @@ func NewSession(session *db.Session, user *User) *Session {
|
||||
return &Session{
|
||||
Id: session.Id,
|
||||
CreatedAt: session.CreatedAt,
|
||||
ExpiresAt: session.ExpiresAt,
|
||||
User: user,
|
||||
}
|
||||
}
|
||||
@@ -59,6 +61,7 @@ type Auth interface {
|
||||
VerifyUserEmail(token string) error
|
||||
|
||||
SignIn(email string, password string) (*Session, error)
|
||||
SignInSession(sessionId string) (*Session, error)
|
||||
SignOut(sessionId string) error
|
||||
|
||||
DeleteAccount(user *User) error
|
||||
@@ -68,7 +71,8 @@ type Auth interface {
|
||||
SendForgotPasswordMail(email string) error
|
||||
ForgotPassword(token string, newPass string) error
|
||||
|
||||
GetUserFromSessionId(sessionId string) (*User, error)
|
||||
// IsCsrfTokenValid(token string, user *User) bool
|
||||
// GetCsrfToken(token string, user *User) bool
|
||||
}
|
||||
|
||||
type AuthImpl struct {
|
||||
@@ -113,6 +117,31 @@ func (service AuthImpl) SignIn(email string, password string) (*Session, error)
|
||||
return NewSession(session, NewUser(user)), nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) SignInSession(sessionId string) (*Session, error) {
|
||||
if sessionId == "" {
|
||||
return nil, ErrSessionIdInvalid
|
||||
}
|
||||
|
||||
sessionDb, err := service.db.GetSession(sessionId)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
if sessionDb.ExpiresAt.After(service.clock.Now()) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
userDb, err := service.db.GetUser(sessionDb.UserId)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
user := NewUser(userDb)
|
||||
session := NewSession(sessionDb, user)
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (service AuthImpl) createSession(userId uuid.UUID) (*db.Session, error) {
|
||||
sessionId, err := service.random.String(32)
|
||||
if err != nil {
|
||||
@@ -125,7 +154,10 @@ func (service AuthImpl) createSession(userId uuid.UUID) (*db.Session, error) {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
session := db.NewSession(sessionId, userId, service.clock.Now())
|
||||
createAt := service.clock.Now()
|
||||
expiresAt := createAt.Add(24 * time.Hour)
|
||||
|
||||
session := db.NewSession(sessionId, userId, createAt, expiresAt)
|
||||
|
||||
err = service.db.InsertSession(session)
|
||||
if err != nil {
|
||||
@@ -251,28 +283,6 @@ func (service AuthImpl) SignOut(sessionId string) error {
|
||||
return service.db.DeleteSession(sessionId)
|
||||
}
|
||||
|
||||
func (service AuthImpl) GetUserFromSessionId(sessionId string) (*User, error) {
|
||||
if sessionId == "" {
|
||||
return nil, ErrSessionIdInvalid
|
||||
}
|
||||
|
||||
session, err := service.db.GetSession(sessionId)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
user, err := service.db.GetUser(session.UserId)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
if session.CreatedAt.Add(time.Duration(8 * time.Hour)).Before(service.clock.Now()) {
|
||||
return nil, nil
|
||||
} else {
|
||||
return NewUser(user), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (service AuthImpl) DeleteAccount(user *User) error {
|
||||
|
||||
err := service.db.DeleteUser(user.Id)
|
||||
|
||||
Reference in New Issue
Block a user