This commit is contained in:
Sakurasan
2023-03-28 22:46:22 +08:00
parent 58835ee0d4
commit 2ba63e226d
6 changed files with 492 additions and 5 deletions

View File

@@ -12,6 +12,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt"
"github.com/google/go-github/v50/github"
"github.com/joho/godotenv"
"golang.org/x/oauth2"
ogithub "golang.org/x/oauth2/github"
"gorm.io/driver/mysql"
@@ -19,15 +20,19 @@ import (
)
var db *gorm.DB
var jwtSecret = []byte(os.Getenv("JWT_SECRET"))
var jwtSecret = []byte("JWT_SECRET")
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
initDB()
router := gin.Default()
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:8080"},
AllowOrigins: []string{"http://localhost:8000"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD"},
AllowHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowCredentials: true,
@@ -40,7 +45,8 @@ func main() {
}
func githubLoginHandler(c *gin.Context) {
state := generateState()
state := "1234567890"
// state := generateState()
// code := generateCode()
// err := storeStateToDB(state, code)
@@ -110,7 +116,7 @@ func githubCallbackHandler(c *gin.Context) {
}
c.SetCookie("token", jwtToken, 60*60*24, "/", "localhost", false, true)
c.Redirect(http.StatusFound, "http://localhost:8080/#/")
c.Redirect(http.StatusFound, "http://localhost:8080/")
}
func initDB() {
@@ -202,3 +208,11 @@ func generateJWTToken(userID int64) (string, error) {
return jwtToken, nil
}
type GithubUser struct {
ID int `json:"id"`
Login string `json:"login"`
Name string `json:"name"`
AvatarURL string `json:"avatar_url"`
Email string `json:"email"`
}