Pod tokeny
This commit is contained in:
parent
e63a859209
commit
a624045281
@ -20,6 +20,7 @@ type User struct {
|
||||
Password string `json:"password"`
|
||||
UserDescription string `json:"userDescription"`
|
||||
Points int `json:points`
|
||||
Token string
|
||||
}
|
||||
|
||||
// Structura karty
|
||||
|
57
backend/tokens.go
Normal file
57
backend/tokens.go
Normal file
@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
)
|
||||
|
||||
var mySigningKey = []byte("DoTokenowDlaUszytkownikow")
|
||||
|
||||
func GenerateJWT(_login string) (string, error) {
|
||||
token := jwt.New(jwt.SigningMethodHS256)
|
||||
claims := token.Claims.(jwt.MapClaims)
|
||||
|
||||
claims["authorized"] = true
|
||||
claims["user"] = _login
|
||||
claims["exp"] = time.Now().Add(time.Minute * 30).Unix()
|
||||
|
||||
tokenString, err := token.SignedString(mySigningKey)
|
||||
if err != nil {
|
||||
fmt.Println("[generateJWT][Error] Nie mozna wygenerować")
|
||||
return "", err
|
||||
}
|
||||
|
||||
return tokenString, nil
|
||||
}
|
||||
|
||||
func isTokenValid() bool, error{
|
||||
//sprawdza czy podany token jest pawidlowy
|
||||
if r.Header["Token"] != nil {
|
||||
token, err := jwt.Parse(r.Header["Token"][0], func(token *jwt.Token) (interface{}, error) { //sprawdzanie tokena
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("There was an error ")
|
||||
}
|
||||
//jesli token jest ok
|
||||
return mySigningKey, nil //zwraca token, do zmiennej 'token'
|
||||
}) //koniec dunkcji zwracajacej token
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return false, err
|
||||
}
|
||||
|
||||
if token.Valid {
|
||||
//jesli token prawidlowy, to zwraca true
|
||||
return true, nil
|
||||
}else{
|
||||
fmt.Println("[isTokenValid] Nieprawidłowy token")
|
||||
return false, nil
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -8,10 +8,20 @@ import (
|
||||
// go get "golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type TokenAPI struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
func getUsersView(c *gin.Context) {
|
||||
// dodanie nowej karty do bzy
|
||||
c.Header("Content-Type", "application/json")
|
||||
fmt.Println("Dodanie do couchDB nowej karty pytania lub odowiedzi")
|
||||
fmt.Println("-----------------------------WYPISANIE TOKENU ------------------------------")
|
||||
|
||||
var token TokenAPI
|
||||
c.Bind(&token)
|
||||
fmt.Println("MOJ TOKEN %s", token.Token)
|
||||
|
||||
// c.Header("Content-Type", "application/json")
|
||||
fmt.Println("Pobieranei listy użytkowników")
|
||||
|
||||
var userList []map[string]interface{}
|
||||
|
||||
@ -83,29 +93,33 @@ func addNewUserView(c *gin.Context) {
|
||||
|
||||
func loginUserView(c *gin.Context) {
|
||||
//logowanie - czy jest w bazie
|
||||
fmt.Println("HEARES--------------------------------")
|
||||
fmt.Println(c.Header)
|
||||
fmt.Println(c.Request)
|
||||
c.Header("Content-Type", "application/json")
|
||||
|
||||
var checkUser User
|
||||
c.Bind(&checkUser)
|
||||
_login := checkUser.Login
|
||||
_password := checkUser.Password
|
||||
_token := checkUser.Token
|
||||
|
||||
fmt.Println("_login: ", _login)
|
||||
fmt.Println("_pasowrd: ", _password)
|
||||
fmt.Println("_token: ", _token)
|
||||
|
||||
if _login == "" {
|
||||
c.JSON(http.StatusOK, "[loginUserView][Error] Nie podano loginu")
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
if _password == "" {
|
||||
c.JSON(http.StatusOK, "[loginUserView][Error] Nie podano hasła")
|
||||
return
|
||||
}
|
||||
validLoginData, err := loginUser(_login, _password)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, "[loginUserView][Error] Nie mozna zalogowac")
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, validLoginData) //true jak zalogowano
|
||||
|
@ -12,6 +12,7 @@ export const store = new Vuex.Store({
|
||||
// { icon: 'exit_to_app', text: 'Wyloguj', route: '/logout'},
|
||||
// { icon: 'get_app', text: 'Zaloguj', route: '/login'},
|
||||
// { icon: 'account_circle', text: 'Rejestracja', route: '/register'},
|
||||
]
|
||||
],
|
||||
userToken : ""
|
||||
}
|
||||
})
|
||||
|
@ -64,7 +64,7 @@ export default {
|
||||
if (this.$refs.form.validate()){ //valinnaj JS
|
||||
|
||||
axios.post("http://127.0.0.1:3000/api/loginUserView" ,
|
||||
{"login": this.userLogin, "password": this.userPassword},
|
||||
{"login": this.userLogin, "password": this.userPassword, "TokenZJS": "Ala"},
|
||||
{ crossdomain: true })
|
||||
.then(response=>{
|
||||
console.log(response.data);
|
||||
|
@ -45,7 +45,14 @@ export default {
|
||||
var objectVue = this;
|
||||
|
||||
var promiseUsers = new Promise(function(resolve, reject) {
|
||||
var allUsersTmp = axios.get("http://127.0.0.1:3000/api/getUsersView" ,{ crossdomain: true })
|
||||
// var allUsersTmp = axios.get("http://127.0.0.1:3000/api/getUsersView" ,{ crossdomain: true }
|
||||
// )
|
||||
|
||||
var allUsersTmp = axios({
|
||||
method: 'get',
|
||||
url: 'http://127.0.0.1:3000/api/getUsersView',
|
||||
params: {'Token': "TokenZJs"}
|
||||
})
|
||||
.then(function(res){
|
||||
console.log("--------------------Users response:------------------------", res.data.allUsers)
|
||||
return res.data.allUsers //zwraca uzytkownikow
|
||||
|
Loading…
Reference in New Issue
Block a user