Skip to content

Commit

Permalink
Fix register
Browse files Browse the repository at this point in the history
Arne Maier committed Dec 6, 2022
1 parent 750e65d commit e6e0658
Showing 3 changed files with 34 additions and 25 deletions.
4 changes: 2 additions & 2 deletions frontend/src/views/security/Register.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { routes } from "../../routes";
import { link } from "svelte-spa-router";
import {link, replace} from "svelte-spa-router";
import { register } from "./security.service";
import { createNotification } from "../../components/Notification/notificationStore";
@@ -9,14 +9,14 @@
e.preventDefault();
register(username)
.then((u) => {
console.log(u);
createNotification(
{
color: "green",
text: `Registrierung '${u.name}' erfolgreich. Jetzt direkt einloggen.`,
},
5
);
replace(routes.Login.link)
})
.catch((err) => {
createNotification({
39 changes: 20 additions & 19 deletions security/userDB.go
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ func (DbCredential) TableName() string {
return "user_credentials"
}

type dbUser struct {
type DbUser struct {
ID uint64 `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
@@ -45,21 +45,21 @@ type dbUser struct {
Credentials []DbCredential `gorm:"foreignKey:UserID"`
}

func (dbUser) TableName() string {
func (DbUser) TableName() string {
return "users"
}

func NewUserDB(db *gorm.DB) *userDB {
db.AutoMigrate(&dbUser{}, &DbCredential{}, &DbAuthenticator{})
db.AutoMigrate(&DbUser{}, &DbCredential{}, &DbAuthenticator{})

return &userDB{
db: db,
}
}

func (u *userDB) GetUser(username string) (*User, error) {
var dbu dbUser
err := u.db.Model(&dbUser{}).Preload("Credentials").First(&dbu, "name = ?", username).Error
var dbu DbUser
err := u.db.Model(&DbUser{}).Preload("Credentials").First(&dbu, "name = ?", username).Error

if err != nil {
return &User{}, fmt.Errorf("error getting user: %s", username)
@@ -69,7 +69,7 @@ func (u *userDB) GetUser(username string) (*User, error) {
}

func (u *userDB) GetAll() ([]*User, error) {
var dbUser []dbUser
var dbUser []DbUser
err := u.db.Find(&dbUser).Error

if err != nil {
@@ -88,13 +88,14 @@ func (u *userDB) AddUser(user *User) (*User, error) {
return u.GetUser(user.Name)
}

func (u *userDB) SaveUser(user *User) {
func (u *userDB) SaveUser(user *User) (*User,error) {
u.db.Save(user.toDBUser())
return u.GetUser(user.Name)
}

func (user User) toDBUser() dbUser {
c := []DbCredential{}
for _, cr := range user.Credentials {
func (u *User) toDBUser() *DbUser {
var c []DbCredential
for _, cr := range u.Credentials {
a := DbAuthenticator{
AAGUID: cr.Authenticator.AAGUID,
SignCount: cr.Authenticator.SignCount,
@@ -103,25 +104,25 @@ func (user User) toDBUser() dbUser {

dbC := DbCredential{
ID: cr.ID,
UserID: user.ID,
UserID: u.ID,
PublicKey: cr.PublicKey,
AttestationType: cr.AttestationType,
Authenticator: a,
}
c = append(c, dbC)
}
dbu := dbUser{
ID: user.ID,
Name: user.Name,
IsApproved: user.IsApproved,
IsAdmin: user.IsAdmin,
dbu := DbUser{
ID: u.ID,
Name: u.Name,
IsApproved: u.IsApproved,
IsAdmin: u.IsAdmin,
Credentials: c,
}
return dbu
return &dbu
}

func (dbu dbUser) toUser() *User {
c := []webauthn.Credential{}
func (dbu *DbUser) toUser() *User {
var c []webauthn.Credential
for _, cr := range dbu.Credentials {
a := webauthn.Authenticator{
AAGUID: cr.Authenticator.AAGUID,
16 changes: 12 additions & 4 deletions security/webauthn.go
Original file line number Diff line number Diff line change
@@ -105,8 +105,12 @@ func (w WebAuthNService) FinishRegistration(c *gin.Context) {
user.AddCredential(*credential)
// TODO: remove after endpoint can change this
user.IsApproved = true
w.userDB.SaveUser(user)
c.Status(http.StatusOK)
user, err = w.userDB.SaveUser(user)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
c.JSON(http.StatusOK, user)
}

func (w *WebAuthNService) StartLogin(c *gin.Context) {
@@ -154,12 +158,16 @@ func (w *WebAuthNService) FinishLogin(c *gin.Context) {
return
}

w.userDB.SaveUser(user)
user, err = w.userDB.SaveUser(user)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}

token := w.jwtService.GenerateToken(*user)

w.jwtService.SetCookie(c, token)
c.Status(http.StatusOK)
c.JSON(http.StatusOK, user)
}

func (w *WebAuthNService) Logout(c *gin.Context) {

0 comments on commit e6e0658

Please sign in to comment.