all repos — flounder @ d083fbf8a06a861d62e355ad64192bd2c313565d

A small site builder for the Gemini protocol

Create admin user on first login

Useful for testing / self-hosting
alex wennerberg alex@alexwennerberg.com
Sun, 31 Jan 2021 02:06:39 -0800
commit

d083fbf8a06a861d62e355ad64192bd2c313565d

parent

68ea2ae1660587e2827ee67aac6e6759ff7bbb1e

1 files changed, 16 insertions(+), 3 deletions(-)

jump to
M db.godb.go

@@ -3,6 +3,7 @@

import ( "crypto/rand" "database/sql" + "golang.org/x/crypto/bcrypt" "io" "io/ioutil" "log"

@@ -191,7 +192,7 @@ return result, nil

} func createTablesIfDNE() { - _, err := DB.Exec(`CREATE TABLE IF NOT EXISTS user ( + _, err := DB.Exec(`CREATE TABLE user ( id INTEGER PRIMARY KEY NOT NULL, username TEXT NOT NULL UNIQUE, email TEXT NOT NULL UNIQUE,

@@ -202,9 +203,21 @@ admin boolean NOT NULL DEFAULT false,

created_at INTEGER DEFAULT (strftime('%s', 'now')), domain TEXT NOT NULL default "", domain_enabled BOOLEAN NOT NULL DEFAULT false -); +);`) + if err == nil { + // on first creation, create admin user with pw admin + hashedPassword, err := bcrypt.GenerateFromPassword([]byte("admin"), 8) // TODO handle error + if err != nil { + log.Fatal(err) + } + _, err = DB.Exec(`INSERT OR IGNORE INTO user (username, email, password_hash, admin) values ('admin', 'default@flounder.local', ?, true)`, hashedPassword) + activateUser("admin") + if err != nil { + log.Fatal(err) + } + } -CREATE TABLE IF NOT EXISTS cookie_key ( + _, err = DB.Exec(`CREATE TABLE IF NOT EXISTS cookie_key ( value TEXT NOT NULL );`) if err != nil {