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

Fix config and google login issue #102

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions config/core-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
JwtValidity int
JwtIssuer string
AllowedOrigin string
Port string
)

func findAndLoadEnv(envFile string) {
Expand Down Expand Up @@ -99,6 +100,9 @@ func loadConfig() {
TokenExpiration = getEnvInt("TokenExpiration")

AllowedOrigin = getEnvVar("ALLOWED_ORIGINS")

Port = getEnvVar("PORT")
JwtValidity = getEnvInt("JWT_VALIDITY_IN_HOURS")
}

func getEnvVar(key string) string {
Expand Down
10 changes: 5 additions & 5 deletions controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
var (
googleOAuthConfig *oauth2.Config
googleConfigMu sync.Mutex
domain = config.Domain
authRedirectUrl = config.AuthRedirectUrl
domain = config.Domain
authRedirectUrl = config.AuthRedirectUrl
)

func getGoogleOAuthConfig() *oauth2.Config {
Expand All @@ -30,9 +30,9 @@ func getGoogleOAuthConfig() *oauth2.Config {

if googleOAuthConfig == nil {
googleOAuthConfig = &oauth2.Config{
ClientID: config.AuthRedirectUrl,
ClientSecret: config.GoogleClientId,
RedirectURL: config.GoogleRedirectUrl,
ClientID: config.GoogleClientId,
ClientSecret: config.GoogleClientSecret,
RedirectURL: config.GoogleRedirectUrl,
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
Expand Down
5 changes: 3 additions & 2 deletions environments/test.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ENV="test"
JWT_SECRET="secret"
JWT_VALIDITY_IN_DAYS=7
JWT_VALIDITY_IN_HOURS=24
iamitprakash marked this conversation as resolved.
Show resolved Hide resolved
JWT_ISSUER="tiny-site-backend"

DOMAIN="localhost"
Expand All @@ -16,4 +16,5 @@ GOOGLE_REDIRECT_URL="http://localhost:8000/v1/auth/google/callback"

Max_Url_Count=10
TokenExpiration=3600
ALLOWED_ORIGINS=*
ALLOWED_ORIGINS=*
PORT=8000
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package main

import (
"flag"
"log"
"os"

"github.com/Real-Dev-Squad/tiny-site-backend/config"
"github.com/Real-Dev-Squad/tiny-site-backend/logger"
"github.com/Real-Dev-Squad/tiny-site-backend/routes"
"github.com/Real-Dev-Squad/tiny-site-backend/utils"
)
Expand All @@ -14,11 +13,11 @@ func main() {
dsn := config.DbUrl
db, err := utils.SetupDBConnection(dsn)
if err != nil {
log.Fatalf("failed to connect to the database: %v", err)
logger.Fatal("failed to connect to the database:", err)
}


port := os.Getenv("PORT")
port := config.Port
if port == "" {
port = "8080"
}
Expand Down
8 changes: 1 addition & 7 deletions utils/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package utils

import (
"errors"
"os"
"strconv"
"time"

"github.com/Real-Dev-Squad/tiny-site-backend/config"
Expand All @@ -18,11 +16,7 @@ func GenerateToken(user *models.User) (string, error) {
issuer := config.JwtIssuer
key := []byte(config.JwtSecret)

tokenValidityInHours, err := strconv.ParseInt(os.Getenv("JWT_VALIDITY_IN_HOURS"), 10, 64)
if err != nil {
return "", err
}

tokenValidityInHours := config.JwtValidity

tokenExpiryTime := time.Now().Add(time.Duration(tokenValidityInHours) * time.Hour).UTC().Format(time.RFC3339)

Expand Down
Loading