token-validator: use cookies instead of localStorage to store auth token

This commit is contained in:
nemunaire 2020-03-01 18:15:19 +01:00
parent 72a4015288
commit a4a7b48a4f
6 changed files with 98 additions and 92 deletions

View file

@ -7,7 +7,7 @@ import (
type Session struct {
Id []byte `json:"id"`
IdStudent int64 `json:"login"`
IdStudent *int64 `json:"login"`
Time time.Time `json:"time"`
}
@ -16,6 +16,17 @@ func getSession(id []byte) (s Session, err error) {
return
}
func NewSession() (Session, error) {
session_id := make([]byte, 255)
if _, err := rand.Read(session_id); err != nil {
return Session{}, err
} else if _, err := DBExec("INSERT INTO student_sessions (id_session, time) VALUES (?, ?)", session_id, time.Now()); err != nil {
return Session{}, err
} else {
return Session{session_id, nil, time.Now()}, nil
}
}
func (student Student) NewSession() (Session, error) {
session_id := make([]byte, 255)
if _, err := rand.Read(session_id); err != nil {
@ -23,10 +34,16 @@ func (student Student) NewSession() (Session, error) {
} else if _, err := DBExec("INSERT INTO student_sessions (id_session, id_student, time) VALUES (?, ?, ?)", session_id, student.Id, time.Now()); err != nil {
return Session{}, err
} else {
return Session{session_id, student.Id, time.Now()}, nil
return Session{session_id, &student.Id, time.Now()}, nil
}
}
func (s Session) SetStudent(student Student) (Session, error) {
s.IdStudent = &student.Id
_, err := s.Update()
return s, err
}
func (s Session) Update() (int64, error) {
if res, err := DBExec("UPDATE student_sessions SET id_student = ?, time = ? WHERE id_session = ?", s.IdStudent, s.Time, s.Id); err != nil {
return 0, err