Account controls and operations
Profile updates, server-side password checks, window-aware rate limits and queryable audit events.
This guide walks through all four capabilities in order. Start with a configured Client; each step builds on the previous one.
1. Start with a configured client
package main
import (
"context"
"github.com/shakibdshy/signet-auth"
)
func main() {
cfg := signet.DefaultConfig()
cfg.Store = myStore // implements signet.Store
cfg.Mailer = myMailer // implements signet.Mailer
cfg.RateLimiter = myLimiter // implements signet.RateLimiter
auth, err := signet.New(cfg)
if err != nil {
panic(err)
}
ctx := context.Background()
_ = auth
_ = ctx
}2. Update a profile
Sign in first and keep the Session token returned by SignIn:
signIn, err := auth.SignIn(ctx, signet.SignInInput{
Email: "ada@example.com", Password: "correct horse",
})
if err != nil {
return err
}
sessionToken := signIn.SessionTokenThen change safe profile fields:
updated, err := auth.UpdateUser(ctx, signet.UpdateUserInput{
UserID: signIn.User.ID,
SessionToken: sessionToken,
Name: "Ada Lovelace",
})
if err != nil {
return err
}
fmt.Println(updated.Email, updated.Name)UpdateUser requires a fresh Session belonging to that User. Email is not accepted here — use the verified two-phase change flow.
3. Set and verify a password
For a User created through passkey, OAuth or a magic link, there is no credential Account yet:
err := auth.SetPassword(ctx, signet.SetPasswordInput{
UserID: userID,
SessionToken: sessionToken,
NewPassword: "a-long-random-password",
})
if err != nil {
return err
}A User who already has a password is rejected; use ChangePassword with the current password instead. SetPassword revokes existing Sessions and Tokens, so sign in again afterwards.
To verify a password without creating a Session, for example before a sensitive server-side action:
ok, err := auth.VerifyPassword(ctx, signet.VerifyPasswordInput{
UserID: userID,
Password: providedPassword,
})
if err != nil {
return err
}
if !ok {
return errors.New("wrong password")
}Unknown Users and wrong passwords both return false. The call is rate-limited with the verify_password key.
4. Add a window-aware rate limiter
Implement the optional adapter:
type windowLimiter struct {
mu sync.Mutex
counts map[string][]time.Time
}
func (l *windowLimiter) Allow(ctx context.Context, key string, limit int, window time.Duration) (bool, error) {
l.mu.Lock()
defer l.mu.Unlock()
now := time.Now()
recent := l.counts[key][:0]
for _, at := range l.counts[key] {
if now.Sub(at) < window {
recent = append(recent, at)
}
}
if len(recent) >= limit {
l.counts[key] = recent
return false, nil
}
l.counts[key] = append(recent, now)
return true, nil
}Attach it before creating the Client and set per-endpoint rules:
cfg.WindowLimiter = &windowLimiter{counts: make(map[string][]time.Time)}
cfg.RateLimitRules = map[string]signet.RateLimitRule{
"signup": {Limit: 7, Window: 2 * time.Minute},
}
auth, err := signet.New(cfg)Without WindowLimiter, the boolean RateLimiter remains authoritative and existing behaviour is unchanged.
5. Query Audit events
Implement a store that both records and queries events:
type auditStore struct {
mu sync.Mutex
events []signet.AuditEvent
}
func (s *auditStore) Record(_ context.Context, event signet.AuditEvent) error {
s.mu.Lock()
defer s.mu.Unlock()
s.events = append(s.events, event)
return nil
}
func (s *auditStore) Query(_ context.Context, in signet.AuditQueryInput) ([]*signet.AuditEvent, error) {
s.mu.Lock()
defer s.mu.Unlock()
out := make([]*signet.AuditEvent, 0, len(s.events))
for _, event := range s.events {
if in.Name != "" && event.Name != in.Name {
continue
}
if in.Outcome != "" && event.Outcome != in.Outcome {
continue
}
clone := event
out = append(out, &clone)
if len(out) >= in.Limit {
break
}
}
return out, nil
}Attach it to the config:
audits := &auditStore{}
cfg.AuditQueryStore = audits
cfg.AuditSink = audits // optional: New wires this automatically when only AuditQueryStore is set
auth, err := signet.New(cfg)Then read events after an action:
events, err := auth.QueryAudit(ctx, sessionToken, signet.AuditQueryInput{
Name: "user_updated",
Limit: 50,
})
if err != nil {
return err
}
for _, event := range events {
fmt.Println(event.Name, event.Outcome, event.UserID)
}A fresh Session is required, results are capped at 100, and known secret metadata fields are omitted.