Skip to content

Commit

Permalink
Merge pull request #88 from Pomog/yurii_dev
Browse files Browse the repository at this point in the history
github login
  • Loading branch information
TartuDen authored Jan 2, 2024
2 parents da5a195 + 4f8c649 commit f361ad2
Show file tree
Hide file tree
Showing 52 changed files with 3,599 additions and 56 deletions.
3 changes: 3 additions & 0 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func main() {
app.MinSubjectLen = 3
app.MaxSubjectLen = 2500
app.LongestSingleWord = "pneumonoultramicroscopicsilicovolcanoconiosis"
app.GitHubClientID = "b383a969da8082007c4c"
app.GitHubClientSecret = "2327f7f8677894e2726f70696d9321399efb6b61"
app.GitHubRedirectURL = "http://localhost:8080/github-callback"

//the list of games that are represented and will be covered on site.
app.GamesList = map[string]string{
Expand Down
3 changes: 3 additions & 0 deletions cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func routes(a *config.AppConfig) http.Handler {

mux.HandleFunc("/create_post_result", handler.Repo.CreatePostResultHandler)

mux.HandleFunc("/login-github", handler.Repo.LoginWithGitHubHandler)
mux.HandleFunc("/github-callback", handler.Repo.CallbackGitHubHandler)

return mux
}

Expand Down
29 changes: 29 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Go Forum Authenticate Web Application

Go Forum Authentication is a vanilla Forum application with additional authentication methods.


### Objectives

The goal of this project is to implement, into your forum, new ways of authentication. You have to be able to register and to login using at least Google and Github authentication tools.

Some examples of authentication means are:

- Facebook
- GitHub
- Google

### Instructions

- To login with Google give contributors you Google e-mail to put your name on google whitelist.
- Your project must have implemented at least the two authentication examples given.
- Your project must be written in **Go**.
- The code must respect the [**good practices**](../../good-practices/README.md).

## Contributing

- Kveber
- Karl-Thomas



15 changes: 15 additions & 0 deletions example/config/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package config

import (
"database/sql"

_ "github.com/mattn/go-sqlite3"
)

var DB *sql.DB

func InitializeDB() (*sql.DB, error) {
var err error
DB, err = sql.Open("sqlite3", "mydb.db")
return DB, err
}
29 changes: 29 additions & 0 deletions example/config/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

import (
"database/sql"
"log"
)

func Run() {
migrate(DB, UserTable)
migrate(DB, PostTable)
migrate(DB, CategoryTable)
migrate(DB, PostCategoryTable)
migrate(DB, PostRatingTable)
migrate(DB, PostRepliesTable)
migrate(DB, PostRepliesRatingTable)
migrate(DB, SessionTable)
}

func migrate(db *sql.DB, query string) {
statement, err := db.Prepare(query)
if err == nil {
_, creationError := statement.Exec()
if creationError != nil {
log.Println(creationError.Error())
}
} else {
log.Println(err.Error())
}
}
88 changes: 88 additions & 0 deletions example/config/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package config

const UserTable = `
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
age INTEGER DEFAULT 0,
gender TEXT,
firstname TEXT,
lastname TEXT,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
provider TEXT NOT NULL
);
`

const PostTable = `
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
user_id INTEGER,
foreign key (user_id) REFERENCES users (id)
);
`

const CategoryTable = `
CREATE TABLE IF NOT EXISTS categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
name_slug TEXT NOT NULL
);
`

const PostCategoryTable = `
CREATE TABLE IF NOT EXISTS posts_category (
id INTEGER PRIMARY KEY AUTOINCREMENT,
post_id INTEGER,
category_id INTEGER,
foreign key (post_id) REFERENCES posts (id),
foreign key (category_id) REFERENCES categories (id)
);
`

const PostRatingTable = `
CREATE TABLE IF NOT EXISTS posts_rating (
id INTEGER PRIMARY KEY AUTOINCREMENT,
post_id INTEGER,
user_id INTEGER,
rating INTEGER,
foreign key (post_id) REFERENCES posts (id),
foreign key (user_id) REFERENCES users (id)
);
`

const PostRepliesTable = `
CREATE TABLE IF NOT EXISTS posts_replies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
post_id INTEGER,
user_id INTEGER,
content TEXT,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
foreign key (post_id) REFERENCES posts (id),
foreign key (user_id) REFERENCES users (id)
);
`
const PostRepliesRatingTable = `
CREATE TABLE IF NOT EXISTS posts_replies_rating (
id INTEGER PRIMARY KEY AUTOINCREMENT,
post_reply_id INTEGER,
user_id INTEGER,
rating INTEGER,
foreign key (post_reply_id) REFERENCES posts_replies (id),
foreign key (user_id) REFERENCES users (id)
);
`

const SessionTable = `
CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
name TEXT,
value TEXT,
expiration DATETIME,
foreign key (user_id) REFERENCES users (id)
);
`
43 changes: 43 additions & 0 deletions example/controller/categoryController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package controller

import (
"encoding/json"
"fmt"
"net/http"

"forum-authentication/types"
)

type CategoryController struct{}

func (_ *CategoryController) CategoryController(w http.ResponseWriter, r *http.Request) {

category_slug := r.URL.Query().Get("slug")

if r.Method == "GET" {
categories := []types.Categories{}
var err error

if category_slug == "" {
categories, err = category.GetCategories()
} else {
categories, err = category.GetCategoryBySlug(category_slug)
}

if err != nil {
fmt.Println(err)
http.Error(w, "Error getting categories", http.StatusInternalServerError)
return
}

categoriesJson, err := json.Marshal(categories)
if err != nil {
http.Error(w, "Error encoding JSON", http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusAccepted)
w.Write(categoriesJson)

}
}
28 changes: 28 additions & 0 deletions example/controller/controllerHelpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package controller

import (
"encoding/json"
"html/template"
"log"
"net/http"
)

func RenderPage(w http.ResponseWriter, templatePath string, data interface{}) {
tmpl, err := template.ParseGlob(templatePath)
if err != nil {
log.Fatal(err)
}

err = tmpl.Execute(w, data)
if err != nil {
log.Fatal(err)
}
}

func RespondWithJSON(w http.ResponseWriter, status int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(data); err != nil {
http.Error(w, "Error with encoding response", http.StatusInternalServerError)
}
}
Loading

0 comments on commit f361ad2

Please sign in to comment.