Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github login #88

Merged
merged 5 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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