Skip to content

Commit d36679d

Browse files
committed
update sign up
1 parent dfa71a9 commit d36679d

File tree

13 files changed

+446
-2
lines changed

13 files changed

+446
-2
lines changed

config/dbConnection.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package config
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"log"
7+
"os"
8+
_ "github.com/lib/pq"
9+
"github.com/joho/godotenv"
10+
)
11+
12+
func Connect() *sql.DB {
13+
// ** DATABASE SETTINGS & CONNECT**
14+
//load environment variables
15+
godotenv.Load()
16+
psql_host := os.Getenv("POSTGRE_HOST")
17+
psql_user := os.Getenv("POSTGRE_USER")
18+
psql_password := os.Getenv("POSTGRE_PASSWORD")
19+
psql_dbname := os.Getenv("POSTGRE_DBNAME")
20+
psql_port := os.Getenv("POSTGRE_PORT")
21+
22+
// connect to postgres
23+
connection := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Taipei", psql_host, psql_user, psql_password, psql_dbname, psql_port)
24+
25+
db, err := sql.Open("postgres", connection)
26+
27+
if err != nil {
28+
log.Fatal(err)
29+
}
30+
31+
if err = db.Ping(); err != nil {
32+
log.Fatal(err)
33+
}
34+
35+
return db
36+
}

database/user/chechUserExist.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package DatabaseUser
2+
3+
import (
4+
"returnone/config"
5+
)
6+
7+
func CheckUserEmailExist(email string) int {
8+
db := config.Connect()
9+
10+
defer db.Close()
11+
12+
var count int
13+
14+
sqlString := `SELECT COUNT(*) FROM users WHERE email = $1;`
15+
db.QueryRow(sqlString,email).Scan(&count)
16+
17+
return count
18+
}
19+
20+
func CheckUserNameExist(user_name string) int {
21+
db := config.Connect()
22+
23+
defer db.Close()
24+
25+
var count int
26+
27+
sqlString := `SELECT COUNT(*) FROM users WHERE user_name = $1;`
28+
db.QueryRow(sqlString,user_name).Scan(&count)
29+
30+
return count
31+
}

database/user/createUser.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package DatabaseUser
2+
3+
import (
4+
"returnone/config"
5+
"returnone/models/user"
6+
Generate "returnone/utils/generate"
7+
"time"
8+
)
9+
10+
func CreateUser(
11+
email string,
12+
hash_password string,
13+
user_name string,
14+
) userModles.UserAccount {
15+
db := config.Connect()
16+
17+
defer db.Close()
18+
19+
20+
now_time := time.Now()
21+
sqlString := `
22+
INSERT INTO users
23+
(id, email, password, user_name, create_at, update_at)
24+
VALUES
25+
($1, $2, $3, $4, $5, $6)
26+
`
27+
28+
user_id := Generate.GenerateUserAccountId()
29+
30+
db.Exec(
31+
sqlString,
32+
user_id, email,
33+
hash_password,
34+
user_name,
35+
now_time,
36+
now_time)
37+
38+
insert_data := userModles.UserAccount{
39+
Id: user_id,
40+
Email: email,
41+
Phone: "",
42+
Phone_country: "",
43+
Password: hash_password,
44+
Email_verify: false,
45+
Phone_verify: false,
46+
Avatar: "",
47+
User_name: user_name,
48+
Github_connect: "",
49+
Google_connect: "",
50+
Create_at: now_time,
51+
Update_at: now_time}
52+
53+
return insert_data
54+
}

go.mod

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
11
module returnone
22

33
go 1.21.4
4+
5+
require (
6+
github.com/andybalholm/brotli v1.0.5 // indirect
7+
github.com/bwmarrin/snowflake v0.3.0 // indirect
8+
github.com/gofiber/fiber/v2 v2.51.0 // indirect
9+
github.com/google/uuid v1.4.0 // indirect
10+
github.com/joho/godotenv v1.5.1 // indirect
11+
github.com/klauspost/compress v1.16.7 // indirect
12+
github.com/lib/pq v1.10.9 // indirect
13+
github.com/mattn/go-colorable v0.1.13 // indirect
14+
github.com/mattn/go-isatty v0.0.20 // indirect
15+
github.com/mattn/go-runewidth v0.0.15 // indirect
16+
github.com/rivo/uniseg v0.2.0 // indirect
17+
github.com/valyala/bytebufferpool v1.0.0 // indirect
18+
github.com/valyala/fasthttp v1.50.0 // indirect
19+
github.com/valyala/tcplisten v1.0.0 // indirect
20+
golang.org/x/crypto v0.16.0 // indirect
21+
golang.org/x/sys v0.15.0 // indirect
22+
)

go.sum

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
2+
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
3+
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
4+
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
5+
github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ=
6+
github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U=
7+
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
8+
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
10+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
11+
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
12+
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
13+
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
14+
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
15+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
16+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
17+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
18+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
19+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
20+
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
21+
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
22+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
23+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
24+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
25+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
26+
github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M=
27+
github.com/valyala/fasthttp v1.50.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
28+
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
29+
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
30+
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
31+
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
32+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
33+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34+
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
35+
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
36+
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
37+
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

main.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
package main
22

3+
import (
4+
"returnone/routes/auth"
5+
"github.com/gofiber/fiber/v2"
6+
"github.com/gofiber/fiber/v2/middleware/logger"
7+
)
8+
39
func main() {
4-
5-
}
10+
app := fiber.New()
11+
12+
// Set logger
13+
app.Use(logger.New(logger.Config{
14+
Format: "[${time}] ${ip} - | ${status} |${latency} | ${method} | ${path} \n",
15+
TimeFormat: "2006/01/02 15:04:05",
16+
TimeZone: "local",
17+
}))
18+
19+
api_v1 := app.Group("/v1")
20+
21+
// set auth controller
22+
auth_group := api_v1.Group("/auth")
23+
auth.Setup(auth_group)
24+
25+
app.Get("/", func(c *fiber.Ctx) error {
26+
return c.SendString("Welcome to returnone backend!")
27+
})
28+
29+
// app Listen
30+
app.Listen(":8080")
31+
32+
}

models/user/user2fa.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package userModles
2+
3+
type User2fa struct {
4+
User_id string `json:"user_id"`
5+
Uhone_2fa bool `json:"phone_2fa"`
6+
Email_2fa bool `json:"email_2fa"`
7+
Totp_2fa bool `json:"totp_2fa"`
8+
Totp string `json:"totp"`
9+
Default_2fa int `json:"default_2fa"`
10+
}

models/user/userAccount.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package userModles
2+
3+
import "time"
4+
5+
type UserAccount struct {
6+
Id string `json:"id"`
7+
Email string `json:"email"`
8+
Phone string `json:"phone"`
9+
Phone_country string `json:"phone_country"`
10+
Password string `json:"password"`
11+
Email_verify bool `json:"email_verify"`
12+
Phone_verify bool `json:"phone_verify"`
13+
Avatar string `json:"avatar"`
14+
User_name string `json:"user_name"`
15+
Github_connect string `json:"github_connect"`
16+
Google_connect string `json:"google_connect"`
17+
Create_at time.Time `json:"create_at"`
18+
Update_at time.Time `json:"update_at"`
19+
}
20+
21+
// CREATE TABLE IF NOT EXISTS users (
22+
// id VARCHAR(255) PRIMARY KEY,
23+
// email VARCHAR(100) NOT NULL,
24+
// phone VARCHAR(50),
25+
// phone_country VARCHAR(25),
26+
// password VARCHAR(255) NOT NULL,
27+
// email_verify BOOLEAN DEFAULT FALSE,
28+
// phone_verify BOOLEAN DEFAULT FALSE,
29+
// avatar VARCHAR(255),
30+
// user_name VARCHAR(30) NOT NULL,
31+
// github_connect VARCHAR(100),
32+
// google_connect VARCHAR(100),
33+
// create_at TIMESTAMP,
34+
// update_at TIMESTAMP
35+
// );

routes/auth/controller.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package auth
2+
3+
import "github.com/gofiber/fiber/v2"
4+
5+
func Setup(app fiber.Router) {
6+
7+
app.Post("/signup", SignUp)
8+
app.Get("/emailexist", EmailExist)
9+
app.Get("/usernameexist", UserNameExist)
10+
}

0 commit comments

Comments
 (0)