Skip to content

Commit

Permalink
user field will be username
Browse files Browse the repository at this point in the history
if our user (developer) wants to store email no problem, but we can not leave some places with email name and others as username

using username we leave flexible

Signed-off-by: Avelino <t@avelino.xxx>
  • Loading branch information
avelino committed Oct 31, 2020
1 parent 5a646f5 commit ac068d6
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var authUpCmd = &cobra.Command{
fmt.Fprintf(os.Stdout, err.Error())
return err
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS public.users (id serial, name text, email text unique, password text, metadata jsonb)")
_, err = db.Exec("CREATE TABLE IF NOT EXISTS public.users (id serial, name text, username text unique, password text, metadata jsonb)")
if err != nil {
fmt.Fprintf(os.Stdout, err.Error())
return err
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func viperCfg() {
viper.SetConfigName(file)
viper.SetConfigType("toml")
viper.SetDefault("auth.enabled", false)
viper.SetDefault("auth.username", "email")
viper.SetDefault("auth.username", "username")
viper.SetDefault("auth.password", "password")
viper.SetDefault("auth.encrypt", "MD5")
viper.SetDefault("auth.type", "body")
Expand Down
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ func Test_Auth(t *testing.T) {
t.Errorf("expected auth.table to be: users, got: %s", cfg.AuthTable)
}

if cfg.AuthUsername != "email" {
t.Errorf("expected auth.username to be: email, got: %s", cfg.AuthUsername)
if cfg.AuthUsername != "username" {
t.Errorf("expected auth.username to be: username, got: %s", cfg.AuthUsername)
}

if cfg.AuthPassword != "password" {
Expand Down
10 changes: 6 additions & 4 deletions controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ type AuthClaims struct {
jwt.StandardClaims
}

// User logged in user representation
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Metadata string `json:"metadata"`
ID int `json:"id"`
Name string `json:"name"`
Username string `json:"username"`
Metadata interface{} `json:"metadata"`
}

// Login representation of data received in authentication
type Login struct {
Username string `json:"username"`
Password string `json:"password"`
Expand Down
2 changes: 1 addition & 1 deletion controllers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Test_basicPasswordCheck(t *testing.T) {
func Test_getSelectQuery(t *testing.T) {
config.Load()

expected := "SELECT * FROM test_users WHERE email=$1 AND password=$2 LIMIT 1"
expected := "SELECT * FROM test_users WHERE username=$1 AND password=$2 LIMIT 1"
query := getSelectQuery()

if query != expected {
Expand Down
4 changes: 2 additions & 2 deletions controllers/testdata/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ create table testjson(name text, data jsonb);
create table testarray(id serial, data character varying(250)[]);
create table test_empty_table(id serial, data character varying(250)[]);
create table test_group_by_table(id serial, name text, age integer, salary int);
CREATE TABLE test_users(id serial, email text, password text);
CREATE TABLE test_users(id serial, username text, password text);

-- Inserts
insert into test (name) values ('prest tester');
Expand All @@ -40,7 +40,7 @@ insert into test_group_by_table(name, age, salary) values ('joao', 20, 1250);
insert into test_group_by_table(name, age, salary) values ('maria', 19, 3999);
insert into test_group_by_table(name, age, salary) values ('gopher', 20, 100);
insert into test_group_by_table(name, age, salary) values ('guitarra humana', 19, 3998);
INSERT INTO test_users(email, password) VALUES('test@postgres.rest', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO test_users(username, password) VALUES('test@postgres.rest', 'e10adc3949ba59abbe56e057f20f883e');

-- Views
create table table_to_view(id serial, name text, celphone text);
Expand Down
4 changes: 2 additions & 2 deletions testdata/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ CREATE TABLE testjson(name text, data jsonb);
CREATE TABLE testarray(id serial, data character varying(250)[]);
CREATE TABLE test_empty_table(id serial, data character varying(250)[]);
CREATE TABLE test_group_by_table(id serial, name text, age integer, salary int);
CREATE TABLE test_users(id serial, email text, password text);
CREATE TABLE test_users(id serial, username text, password text);

-- Inserts
INSERT INTO test (name) VALUES ('prest tester');
Expand All @@ -39,7 +39,7 @@ INSERT INTO test_group_by_table(name, age, salary) VALUES('joao', 20, 1250);
INSERT INTO test_group_by_table(name, age, salary) VALUES('maria', 19, 3999);
INSERT INTO test_group_by_table(name, age, salary) VALUES('gopher', 20, 100);
INSERT INTO test_group_by_table(name, age, salary) VALUES('guitarra humana', 19, 3998);
INSERT INTO test_users(email, password) VALUES('test@postgres.rest', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO test_users(username, password) VALUES('test@postgres.rest', 'e10adc3949ba59abbe56e057f20f883e');

-- Views
CREATE TABLE table_to_view(id serial, name text, celphone text);
Expand Down

0 comments on commit ac068d6

Please sign in to comment.