Skip to content

Commit aa00906

Browse files
committed
feat: Implement user authentication and management API
- Added user registration, login, email verification, password reset, and profile retrieval functionalities. - Created handler functions for each endpoint in `internal/user/handler.go`. - Developed corresponding unit tests for handler functions in `internal/user/handler_test.go`. - Introduced a repository layer for user data management in `internal/user/repository.go`. - Implemented service layer for business logic in `internal/user/service.go`. - Added JWT generation and parsing functionality in `pkg/jwt/jwt.go`. - Created DTOs for request and response payloads in `pkg/dto/auth.go`. - Defined custom error handling in `pkg/errors/errors.go`. - Established user and social account models in `pkg/models/user.go` and `pkg/models/social_account.go`. - Added shell scripts for running the API and testing endpoints.
0 parents  commit aa00906

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+5836
-0
lines changed

.air.toml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
root = "."
2+
testdata_dir = "testdata"
3+
tmp_dir = "tmp"
4+
5+
[build]
6+
args_bin = []
7+
bin = "./tmp/main.exe"
8+
cmd = "go build -o ./tmp/main.exe ./cmd/api"
9+
delay = 1000
10+
exclude_dir = ["assets", "tmp", "vendor", "testdata", "node_modules", ".git", "docs"]
11+
exclude_file = []
12+
exclude_regex = ["_test.go"]
13+
exclude_unchanged = false
14+
follow_symlink = false
15+
full_bin = ""
16+
include_dir = []
17+
include_ext = ["go", "tpl", "tmpl", "html"]
18+
include_file = []
19+
kill_delay = "0s"
20+
log = "build-errors.log"
21+
poll = true
22+
poll_interval = 1000
23+
rerun = false
24+
rerun_delay = 500
25+
send_interrupt = false
26+
stop_on_root = false
27+
28+
[color]
29+
app = ""
30+
build = "yellow"
31+
main = "magenta"
32+
runner = "green"
33+
watcher = "cyan"
34+
35+
[log]
36+
main_only = false
37+
time = false
38+
39+
[misc]
40+
clean_on_exit = false
41+
42+
[screen]
43+
clear_on_rebuild = false
44+
keep_scroll = true

.env.example

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# .env.example - Environment variable template for auth_api
2+
3+
DB_HOST=localhost
4+
DB_PORT=5432
5+
DB_USER=postgres
6+
DB_PASSWORD=your_db_password
7+
DB_NAME=auth_db
8+
9+
JWT_SECRET=your_jwt_secret
10+
ACCESS_TOKEN_EXPIRATION_MINUTES=15
11+
REFRESH_TOKEN_EXPIRATION_HOURS=720
12+
13+
REDIS_ADDR=localhost:6379
14+
REDIS_PASSWORD=your_redis_password
15+
REDIS_DB=0
16+
17+
GOOGLE_CLIENT_ID=your_google_client_id
18+
GOOGLE_CLIENT_SECRET=your_google_client_secret
19+
GOOGLE_REDIRECT_URL=http://localhost:8080/auth/google/callback
20+
21+
FACEBOOK_CLIENT_ID=your_facebook_client_id
22+
FACEBOOK_CLIENT_SECRET=your_facebook_client_secret
23+
FACEBOOK_REDIRECT_URL=http://localhost:8080/auth/facebook/callback
24+
25+
GITHUB_CLIENT_ID=your_github_client_id
26+
GITHUB_CLIENT_SECRET=your_github_client_secret
27+
GITHUB_REDIRECT_URL=http://localhost:8080/auth/github/callback
28+
29+
EMAIL_HOST=smtp.example.com
30+
EMAIL_PORT=587
31+
EMAIL_USERNAME=your_email@example.com
32+
EMAIL_PASSWORD=your_email_password
33+
EMAIL_FROM=your_email@example.com

.github/workflows/ci.yml

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
name: CI/CD Pipeline
2+
3+
on:
4+
push:
5+
branches: [main, develop]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
test:
11+
name: Test
12+
runs-on: ubuntu-latest
13+
14+
services:
15+
postgres:
16+
image: postgres:15
17+
env:
18+
POSTGRES_PASSWORD: postgres
19+
POSTGRES_DB: auth_test
20+
options: >-
21+
--health-cmd pg_isready
22+
--health-interval 10s
23+
--health-timeout 5s
24+
--health-retries 5
25+
ports:
26+
- 5432:5432
27+
28+
redis:
29+
image: redis:7
30+
options: >-
31+
--health-cmd "redis-cli ping"
32+
--health-interval 10s
33+
--health-timeout 5s
34+
--health-retries 5
35+
ports:
36+
- 6379:6379
37+
38+
steps:
39+
- name: Checkout code
40+
uses: actions/checkout@v4
41+
42+
- name: Set up Go
43+
uses: actions/setup-go@v4
44+
with:
45+
go-version: "1.22"
46+
47+
- name: Cache Go modules
48+
uses: actions/cache@v3
49+
with:
50+
path: ~/go/pkg/mod
51+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
52+
restore-keys: |
53+
${{ runner.os }}-go-
54+
55+
- name: Download dependencies
56+
run: go mod download
57+
58+
- name: Run unit tests
59+
env:
60+
JWT_SECRET: testsecret
61+
ACCESS_TOKEN_EXPIRATION_MINUTES: 15
62+
REFRESH_TOKEN_EXPIRATION_HOURS: 720
63+
DB_HOST: localhost
64+
DB_PORT: 5432
65+
DB_USER: postgres
66+
DB_PASSWORD: postgres
67+
DB_NAME: auth_test
68+
REDIS_ADDR: localhost:6379
69+
REDIS_PASSWORD: ""
70+
REDIS_DB: 0
71+
run: go test -v -race -coverprofile=coverage.out ./...
72+
73+
- name: Upload coverage to Codecov
74+
uses: codecov/codecov-action@v3
75+
with:
76+
file: ./coverage.out
77+
flags: unittests
78+
name: codecov-umbrella
79+
80+
build:
81+
name: Build
82+
runs-on: ubuntu-latest
83+
needs: test
84+
85+
steps:
86+
- name: Checkout code
87+
uses: actions/checkout@v4
88+
89+
- name: Set up Go
90+
uses: actions/setup-go@v4
91+
with:
92+
go-version: "1.22"
93+
94+
- name: Build application
95+
run: go build -v ./cmd/api
96+
97+
- name: Build Docker image
98+
run: docker build -t auth-api:${{ github.sha }} .
99+
100+
- name: Save Docker image
101+
run: docker save auth-api:${{ github.sha }} | gzip > auth-api.tar.gz
102+
103+
- name: Upload Docker image artifact
104+
uses: actions/upload-artifact@v3
105+
with:
106+
name: docker-image
107+
path: auth-api.tar.gz
108+
109+
security-scan:
110+
name: Security Scan
111+
runs-on: ubuntu-latest
112+
needs: test
113+
114+
steps:
115+
- name: Checkout code
116+
uses: actions/checkout@v4
117+
118+
- name: Set up Go
119+
uses: actions/setup-go@v4
120+
with:
121+
go-version: "1.22"
122+
123+
- name: Run Gosec Security Scanner
124+
run: |
125+
go install github.com/securecodewarrior/gosec/v2/cmd/gosec@latest
126+
gosec ./...
127+
128+
- name: Run Nancy vulnerability scanner
129+
run: |
130+
go install github.com/sonatypecommunity/nancy@latest
131+
go list -json -deps ./... | nancy sleuth
132+
133+
deploy-staging:
134+
name: Deploy to Staging
135+
runs-on: ubuntu-latest
136+
needs: [test, build, security-scan]
137+
if: github.ref == 'refs/heads/develop'
138+
environment: staging
139+
140+
steps:
141+
- name: Checkout code
142+
uses: actions/checkout@v4
143+
144+
- name: Download Docker image artifact
145+
uses: actions/download-artifact@v3
146+
with:
147+
name: docker-image
148+
149+
- name: Load Docker image
150+
run: docker load < auth-api.tar.gz
151+
152+
- name: Deploy to staging
153+
run: |
154+
echo "Deploying to staging environment..."
155+
# Here you would add your actual deployment commands
156+
# For example, using kubectl, docker-compose, or cloud provider CLI
157+
echo "Deployment completed successfully"
158+
159+
deploy-production:
160+
name: Deploy to Production
161+
runs-on: ubuntu-latest
162+
needs: [test, build, security-scan]
163+
if: github.ref == 'refs/heads/main'
164+
environment: production
165+
166+
steps:
167+
- name: Checkout code
168+
uses: actions/checkout@v4
169+
170+
- name: Download Docker image artifact
171+
uses: actions/download-artifact@v3
172+
with:
173+
name: docker-image
174+
175+
- name: Load Docker image
176+
run: docker load < auth-api.tar.gz
177+
178+
- name: Deploy to production
179+
run: |
180+
echo "Deploying to production environment..."
181+
# Here you would add your actual deployment commands
182+
# For example, using kubectl, docker-compose, or cloud provider CLI
183+
echo "Production deployment completed successfully"

.gitignore

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories
15+
vendor/
16+
17+
# Go workspace file
18+
go.work
19+
20+
# Environment variables and configuration files
21+
.env
22+
.env.local
23+
.env.development
24+
.env.test
25+
.env.production
26+
27+
# Logs
28+
*.log
29+
30+
# Runtime data
31+
pids
32+
*.pid
33+
*.seed
34+
*.pid.lock
35+
36+
# IDE and editor files
37+
.vscode/
38+
.idea/
39+
*.swp
40+
*.swo
41+
*~
42+
43+
# OS generated files
44+
.DS_Store
45+
.DS_Store?
46+
._*
47+
.Spotlight-V100
48+
.Trashes
49+
ehthumbs.db
50+
Thumbs.db
51+
52+
# Build outputs
53+
/api
54+
/main
55+
/auth_api
56+
/cmd/api/api
57+
58+
# Test coverage
59+
coverage.out
60+
coverage.html
61+
62+
# Air live reload tool (for hot reloading during development)
63+
tmp/
64+
*.air.toml.bak
65+
.air.toml.bak
66+
67+
# Build artifacts
68+
bin/
69+
*.exe
70+
71+
# Batch files (optional - remove if you want to track run.bat)
72+
# *.bat
73+
74+
# Delve debugger
75+
__debug_bin
76+
77+
# Database files (for local development)
78+
*.db
79+
*.sqlite
80+
*.sqlite3
81+
82+
# Application specific temporary files
83+
uploads/
84+
storage/
85+
cache/

Dockerfile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Use the official Golang image as a base image
2+
FROM golang:1.22-alpine AS builder
3+
4+
# Set the current working directory inside the container
5+
WORKDIR /app
6+
7+
# Set Go toolchain to local to prevent version conflicts
8+
ENV GOTOOLCHAIN=local
9+
10+
# Copy go.mod and go.sum files and download dependencies
11+
COPY go.mod go.sum ./
12+
RUN go mod download
13+
14+
# Copy the source code into the container
15+
COPY . .
16+
17+
# Build the Go application
18+
RUN go build -o /go-auth-api ./cmd/api
19+
20+
# Use a minimal image for the final stage
21+
FROM alpine:latest
22+
23+
# Install ca-certificates for HTTPS connections
24+
RUN apk --no-cache add ca-certificates
25+
26+
# Set the current working directory inside the container
27+
WORKDIR /root/
28+
29+
# Copy the built binary from the builder stage
30+
COPY --from=builder /go-auth-api .
31+
32+
# Expose the port the application runs on
33+
EXPOSE 8080
34+
35+
# Run the application
36+
CMD ["./go-auth-api"]

0 commit comments

Comments
 (0)