WORKING <3
This commit is contained in:
parent
183d44a7f6
commit
460a87ed72
|
@ -1,6 +1,7 @@
|
||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"../forms"
|
"../forms"
|
||||||
|
@ -37,6 +38,8 @@ func (ctrl ArticleController) Create(c *gin.Context) {
|
||||||
|
|
||||||
articleID, err := articleModel.Create(userID, articleForm)
|
articleID, err := articleModel.Create(userID, articleForm)
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
if articleID > 0 && err != nil {
|
if articleID > 0 && err != nil {
|
||||||
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Article could not be created", "error": err.Error()})
|
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Article could not be created", "error": err.Error()})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
|
|
|
@ -51,12 +51,13 @@ func (ctrl UserController) Signin(c *gin.Context) {
|
||||||
user, err := userModel.Signin(signinForm)
|
user, err := userModel.Signin(signinForm)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
session := sessions.Default(c)
|
session := sessions.Default(c)
|
||||||
|
|
||||||
session.Set("user_id", user.ID)
|
session.Set("user_id", user.ID)
|
||||||
session.Set("user_email", user.Email)
|
session.Set("user_email", user.Email)
|
||||||
session.Set("user_name", user.Name)
|
session.Set("user_name", user.Name)
|
||||||
session.Save()
|
session.Save()
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "User signed in", "user": user.ID})
|
// c.JSON(http.StatusOK, gin.H{"message": "User signed in", "user": user.ID})
|
||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid signin details", "error": err.Error()})
|
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid signin details", "error": err.Error()})
|
||||||
}
|
}
|
||||||
|
@ -96,6 +97,7 @@ func (ctrl UserController) Signup(c *gin.Context) {
|
||||||
//Signout ...
|
//Signout ...
|
||||||
func (ctrl UserController) Signout(c *gin.Context) {
|
func (ctrl UserController) Signout(c *gin.Context) {
|
||||||
session := sessions.Default(c)
|
session := sessions.Default(c)
|
||||||
|
|
||||||
session.Clear()
|
session.Clear()
|
||||||
session.Save()
|
session.Save()
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "Signed out..."})
|
c.JSON(http.StatusOK, gin.H{"message": "Signed out..."})
|
||||||
|
|
20
gin/main.go
20
gin/main.go
|
@ -17,17 +17,10 @@ import (
|
||||||
//CORSMiddleware ...
|
//CORSMiddleware ...
|
||||||
func CORSMiddleware() gin.HandlerFunc {
|
func CORSMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
// c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080")
|
||||||
// c.Writer.Header().Set("Access-Control-Max-Age", "86400")
|
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||||
// c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
|
||||||
// c.Writer.Header().Set("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding, x-access-token")
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
|
||||||
// c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
|
|
||||||
// c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
||||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
||||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
|
|
||||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
|
|
||||||
|
|
||||||
|
|
||||||
if c.Request.Method == "OPTIONS" {
|
if c.Request.Method == "OPTIONS" {
|
||||||
fmt.Println("OPTIONS")
|
fmt.Println("OPTIONS")
|
||||||
|
@ -40,11 +33,8 @@ func CORSMiddleware() gin.HandlerFunc {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
store := cookie.NewStore([]byte("secret"))
|
store := cookie.NewStore([]byte("secret"))
|
||||||
r.Use(sessions.Sessions("gin-boilerplate-session", store))
|
r.Use(sessions.Sessions("test_seesion", store))
|
||||||
fmt.Println(store)
|
|
||||||
|
|
||||||
r.Use(CORSMiddleware())
|
r.Use(CORSMiddleware())
|
||||||
|
|
||||||
db.Init()
|
db.Init()
|
||||||
|
|
|
@ -10,8 +10,8 @@ import (
|
||||||
|
|
||||||
//Article ...
|
//Article ...
|
||||||
type Article struct {
|
type Article struct {
|
||||||
ID int64 `db:"id, primarykey, autoincrement" json:"id"`
|
ID int64 `db:"id, primarykey, autoincrement" json:"id"`
|
||||||
UserID string `db:"user_id" json:"-"`
|
UserID string `db:"user_id" json:"-"`
|
||||||
Title string `db:"title" json:"title"`
|
Title string `db:"title" json:"title"`
|
||||||
Content string `db:"content" json:"content"`
|
Content string `db:"content" json:"content"`
|
||||||
UpdatedAt int64 `db:"updated_at" json:"updated_at"`
|
UpdatedAt int64 `db:"updated_at" json:"updated_at"`
|
||||||
|
@ -23,18 +23,18 @@ type Article struct {
|
||||||
type ArticleModel struct{}
|
type ArticleModel struct{}
|
||||||
|
|
||||||
//Create ...
|
//Create ...
|
||||||
func (m ArticleModel) Create(userID int, form forms.ArticleForm) (articleID int64, err error) {
|
func (m ArticleModel) Create(userID int64, form forms.ArticleForm) (articleID int64, err error) {
|
||||||
getDb := db.GetDB()
|
getDb := db.GetDB()
|
||||||
|
|
||||||
userModel := new(UserModel)
|
userModel := new(UserModel)
|
||||||
|
|
||||||
checkUser, err := userModel.One(userID)
|
checkUser, err := userModel.One(int(userID))
|
||||||
|
|
||||||
if err != nil && checkUser.ID > 0 {
|
if err != nil && checkUser.ID > 0 {
|
||||||
return 0, errors.New("User doesn't exist")
|
return 0, errors.New("User doesn't exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = getDb.Exec("INSERT INTO public.article(user_id, title, content, updated_at, created_at) VALUES($1, $2, $3, $4, $5,) RETURNING id", userID, form.Title, form.Content, time.Now().Unix(), time.Now().Unix())
|
_, err = getDb.Exec("INSERT INTO public.article (user_id, title, content, updated_at, created_at) VALUES($1, $2, $3, $4, $5) RETURNING id", userID, form.Title, form.Content, time.Now().Unix(), time.Now().Unix())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|
|
@ -12,6 +12,7 @@ export const articleService = {
|
||||||
function addArticle(article) {
|
function addArticle(article) {
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(article)
|
body: JSON.stringify(article)
|
||||||
};
|
};
|
||||||
|
@ -22,6 +23,7 @@ function addArticle(article) {
|
||||||
function getAll() {
|
function getAll() {
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
credentials: 'include',
|
||||||
headers: authHeader()
|
headers: authHeader()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -31,6 +33,7 @@ function getAll() {
|
||||||
function getById(id) {
|
function getById(id) {
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
credentials: 'include',
|
||||||
headers: authHeader()
|
headers: authHeader()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -40,6 +43,7 @@ function getById(id) {
|
||||||
function update(user) {
|
function update(user) {
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
|
credentials: 'include',
|
||||||
headers: { ...authHeader(), 'Content-Type': 'application/json' },
|
headers: { ...authHeader(), 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(user)
|
body: JSON.stringify(user)
|
||||||
};
|
};
|
||||||
|
@ -50,6 +54,7 @@ function update(user) {
|
||||||
function _delete(id) {
|
function _delete(id) {
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
|
credentials: 'include',
|
||||||
headers: authHeader()
|
headers: authHeader()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ function login(email, password) {
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
credentials: 'include',
|
||||||
body: JSON.stringify({ email, password })
|
body: JSON.stringify({ email, password })
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -30,6 +31,7 @@ function login(email, password) {
|
||||||
function register(user) {
|
function register(user) {
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(user)
|
body: JSON.stringify(user)
|
||||||
};
|
};
|
||||||
|
@ -43,6 +45,7 @@ function logout() {
|
||||||
|
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
credentials: 'include',
|
||||||
};
|
};
|
||||||
|
|
||||||
return fetch(`${config.apiUrl}user/signout`, requestOptions)
|
return fetch(`${config.apiUrl}user/signout`, requestOptions)
|
||||||
|
@ -52,6 +55,7 @@ function logout() {
|
||||||
function getAll() {
|
function getAll() {
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
credentials: 'include',
|
||||||
headers: authHeader()
|
headers: authHeader()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -61,6 +65,7 @@ function getAll() {
|
||||||
function getById(id) {
|
function getById(id) {
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
credentials: 'include',
|
||||||
headers: authHeader()
|
headers: authHeader()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -70,6 +75,7 @@ function getById(id) {
|
||||||
function update(user) {
|
function update(user) {
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
|
credentials: 'include',
|
||||||
headers: { ...authHeader(), 'Content-Type': 'application/json' },
|
headers: { ...authHeader(), 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(user)
|
body: JSON.stringify(user)
|
||||||
};
|
};
|
||||||
|
@ -80,6 +86,7 @@ function update(user) {
|
||||||
function _delete(id) {
|
function _delete(id) {
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
|
credentials: 'include',
|
||||||
headers: authHeader()
|
headers: authHeader()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue