增加密码修改、会话退出和登录记录接口
This commit is contained in:
1 parent
225ce96844
commit
e05c930a9d
9 files changed
+319
-2
No files matched your search
@@ -87,12 +87,19 @@ func (s *Store) Open(ctx context.Context, register bool, username, password, nam
|
||||
return Auth{}, err
|
||||
}
|
||||
}
|
||||
var currentHash string
|
||||
if err = tx.QueryRow(ctx, "SELECT password_hash FROM users WHERE id=$1 FOR UPDATE", result.User.ID).Scan(¤tHash); err != nil {
|
||||
return Auth{}, err
|
||||
}
|
||||
if currentHash != hash {
|
||||
return Auth{}, ErrCredentials
|
||||
}
|
||||
|
||||
row := tx.QueryRow(ctx, `
|
||||
INSERT INTO devices(user_id, public_key, name, platform)
|
||||
VALUES ($1,$2,$3,$4)
|
||||
ON CONFLICT (user_id, public_key) DO UPDATE
|
||||
SET last_active_at=now(), name=EXCLUDED.name
|
||||
SET last_active_at=now(), name=CASE WHEN devices.name_custom THEN devices.name ELSE EXCLUDED.name END
|
||||
WHERE devices.revoked_at IS NULL AND devices.platform=EXCLUDED.platform
|
||||
RETURNING id, name, platform, created_at, last_active_at, revoked_at
|
||||
`, result.User.ID, publicKey, name, platform)
|
||||
@@ -107,6 +114,14 @@ func (s *Store) Open(ctx context.Context, register bool, username, password, nam
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO sessions VALUES ($1,$2,$3)", digest[:], result.Device.ID, time.Now().Add(s.ttl)); err != nil {
|
||||
return Auth{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO login_history(user_id,device_id,device_name,platform) VALUES ($1,$2,$3,$4)`,
|
||||
result.User.ID, result.Device.ID, result.Device.Name, result.Device.Platform); err != nil {
|
||||
return Auth{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM login_history WHERE user_id=$1 AND id NOT IN
|
||||
(SELECT id FROM login_history WHERE user_id=$1 ORDER BY id DESC LIMIT 50)`, result.User.ID); err != nil {
|
||||
return Auth{}, err
|
||||
}
|
||||
result.Device.SignedIn = true
|
||||
return result, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user