WORKING <3

This commit is contained in:
Gerard.S 2019-05-31 00:17:01 +02:00
parent 183d44a7f6
commit 460a87ed72
6 changed files with 28 additions and 21 deletions

View File

@ -1,6 +1,7 @@
package controllers
import (
"fmt"
"strconv"
"../forms"
@ -37,6 +38,8 @@ func (ctrl ArticleController) Create(c *gin.Context) {
articleID, err := articleModel.Create(userID, articleForm)
fmt.Println(err)
if articleID > 0 && err != nil {
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Article could not be created", "error": err.Error()})
c.Abort()

View File

@ -51,12 +51,13 @@ func (ctrl UserController) Signin(c *gin.Context) {
user, err := userModel.Signin(signinForm)
if err == nil {
session := sessions.Default(c)
session.Set("user_id", user.ID)
session.Set("user_email", user.Email)
session.Set("user_name", user.Name)
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 {
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 ...
func (ctrl UserController) Signout(c *gin.Context) {
session := sessions.Default(c)
session.Clear()
session.Save()
c.JSON(http.StatusOK, gin.H{"message": "Signed out..."})

View File

@ -17,17 +17,10 @@ import (
//CORSMiddleware ...
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
// c.Writer.Header().Set("Access-Control-Max-Age", "86400")
// c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
// 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-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")
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080")
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" {
fmt.Println("OPTIONS")
@ -40,11 +33,8 @@ func CORSMiddleware() gin.HandlerFunc {
func main() {
r := gin.Default()
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("gin-boilerplate-session", store))
fmt.Println(store)
r.Use(sessions.Sessions("test_seesion", store))
r.Use(CORSMiddleware())
db.Init()

View File

@ -10,8 +10,8 @@ import (
//Article ...
type Article struct {
ID int64 `db:"id, primarykey, autoincrement" json:"id"`
UserID string `db:"user_id" json:"-"`
ID int64 `db:"id, primarykey, autoincrement" json:"id"`
UserID string `db:"user_id" json:"-"`
Title string `db:"title" json:"title"`
Content string `db:"content" json:"content"`
UpdatedAt int64 `db:"updated_at" json:"updated_at"`
@ -23,18 +23,18 @@ type Article struct {
type ArticleModel struct{}
//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()
userModel := new(UserModel)
checkUser, err := userModel.One(userID)
checkUser, err := userModel.One(int(userID))
if err != nil && checkUser.ID > 0 {
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 {
return 0, err

View File

@ -12,6 +12,7 @@ export const articleService = {
function addArticle(article) {
const requestOptions = {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(article)
};
@ -22,6 +23,7 @@ function addArticle(article) {
function getAll() {
const requestOptions = {
method: 'GET',
credentials: 'include',
headers: authHeader()
};
@ -31,6 +33,7 @@ function getAll() {
function getById(id) {
const requestOptions = {
method: 'GET',
credentials: 'include',
headers: authHeader()
};
@ -40,6 +43,7 @@ function getById(id) {
function update(user) {
const requestOptions = {
method: 'PUT',
credentials: 'include',
headers: { ...authHeader(), 'Content-Type': 'application/json' },
body: JSON.stringify(user)
};
@ -50,6 +54,7 @@ function update(user) {
function _delete(id) {
const requestOptions = {
method: 'DELETE',
credentials: 'include',
headers: authHeader()
};

View File

@ -15,6 +15,7 @@ function login(email, password) {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ email, password })
};
@ -30,6 +31,7 @@ function login(email, password) {
function register(user) {
const requestOptions = {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(user)
};
@ -43,6 +45,7 @@ function logout() {
const requestOptions = {
method: 'GET',
credentials: 'include',
};
return fetch(`${config.apiUrl}user/signout`, requestOptions)
@ -52,6 +55,7 @@ function logout() {
function getAll() {
const requestOptions = {
method: 'GET',
credentials: 'include',
headers: authHeader()
};
@ -61,6 +65,7 @@ function getAll() {
function getById(id) {
const requestOptions = {
method: 'GET',
credentials: 'include',
headers: authHeader()
};
@ -70,6 +75,7 @@ function getById(id) {
function update(user) {
const requestOptions = {
method: 'PUT',
credentials: 'include',
headers: { ...authHeader(), 'Content-Type': 'application/json' },
body: JSON.stringify(user)
};
@ -80,6 +86,7 @@ function update(user) {
function _delete(id) {
const requestOptions = {
method: 'DELETE',
credentials: 'include',
headers: authHeader()
};