add jwt authentication, and fix cours for next js
This commit is contained in:
1 parent
3026f89f2e
commit
05b8d55d0e
17 files changed
+471
-64
No files matched your search
@@ -13,6 +13,7 @@ type Bd struct {
|
||||
filmsrepo *Filmsrepo
|
||||
siriesrepo *Siriesrepo
|
||||
userrepo *Userrepo
|
||||
tokenrepo *Tokenrepo
|
||||
}
|
||||
|
||||
func New(config *ConfigBD) *Bd {
|
||||
@@ -68,3 +69,13 @@ func (b *Bd) User() *Userrepo {
|
||||
}
|
||||
return b.userrepo
|
||||
}
|
||||
|
||||
func (b *Bd) Token() *Tokenrepo {
|
||||
if b.tokenrepo != nil {
|
||||
return b.tokenrepo
|
||||
}
|
||||
b.tokenrepo = &Tokenrepo{
|
||||
db: *b,
|
||||
}
|
||||
return b.tokenrepo
|
||||
}
|
||||
@@ -5,6 +5,14 @@ type User struct {
|
||||
Login string `json:"login"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Avatar_Url string
|
||||
Avatar_Url *string
|
||||
AccesToken string
|
||||
PermisionLVL int
|
||||
}
|
||||
|
||||
type UserData struct {
|
||||
Login string
|
||||
Email string
|
||||
PermisionLVL int
|
||||
AccesToken string
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package bd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Tokenrepo struct {
|
||||
db Bd
|
||||
}
|
||||
|
||||
func (t *Tokenrepo) Create(login string, refreshToken string) error {
|
||||
sql := fmt.Sprint("INSERT INTO tokens (login, refreshtoken) VALUES($1, $2)")
|
||||
_, err := t.db.db.Exec(context.Background(), sql, login, refreshToken)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Tokenrepo) FindByLogin(login string) (*string, error) {
|
||||
var refreshToken string
|
||||
sql := fmt.Sprint("Select refreshtoken from tokens WHERE login = $1")
|
||||
rows, err := t.db.db.Query(context.Background(), sql, login)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
err = rows.Scan(&refreshToken)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &refreshToken, nil
|
||||
|
||||
}
|
||||
|
||||
func (t *Tokenrepo) FindByToken(token string) (*string, error) {
|
||||
var login string
|
||||
sql := fmt.Sprint("Select login from tokens WHERE refreshtoken = $1")
|
||||
rows, err := t.db.db.Query(context.Background(), sql, token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
err = rows.Scan(&login)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &login, nil
|
||||
}
|
||||
|
||||
func (t *Tokenrepo) DeleteByLogin(login string) error {
|
||||
sql := fmt.Sprint("DELETE FROM tokens WHERE login = $1")
|
||||
_, err := t.db.db.Exec(context.Background(), sql, login)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
+16
-4
@@ -12,8 +12,8 @@ type Userrepo struct {
|
||||
}
|
||||
|
||||
func (u *Userrepo) Create(user *model.User) error {
|
||||
_, err := u.db.db.Exec(context.Background(),
|
||||
"INSERT INTO users (login, email, password, permisionlvl) VALUES($1, $2, $3, $4)", user.Login, user.Email, user.Password, user.PermisionLVL)
|
||||
sql := fmt.Sprint("INSERT INTO users (login, email, password, permisionlvl) VALUES($1, $2, $3, $4)")
|
||||
_, err := u.db.db.Exec(context.Background(), sql, user.Login, user.Email, user.Password, user.PermisionLVL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -60,6 +60,18 @@ func (u *Userrepo) FindByEmail(email string) (*model.User, error) {
|
||||
|
||||
}
|
||||
|
||||
func (u *Userrepo) FindByAll() (*model.User, error) {
|
||||
return nil, nil
|
||||
func (u *Userrepo) FindByLoginPas(login string) (*model.User, error) {
|
||||
var user model.User
|
||||
rows, err := u.db.db.Query(context.Background(), "SELECT login, password, permisionlvl, email FROM users WHERE login = $1", login)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&user.Login, &user.Password, &user.PermisionLVL, &user.Email)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
Reference in new issue
Block a user