Skip to content

Commit f79e002

Browse files
[INIT]: initial Commit
0 parents  commit f79e002

File tree

10 files changed

+464
-0
lines changed

10 files changed

+464
-0
lines changed

.gitignore

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig
2+
# Created by https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,go
3+
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,visualstudiocode,go
4+
5+
### Go ###
6+
# If you prefer the allow list template instead of the deny list, see community template:
7+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
8+
#
9+
# Binaries for programs and plugins
10+
*.exe
11+
*.exe~
12+
*.dll
13+
*.so
14+
*.dylib
15+
16+
# Test binary, built with `go test -c`
17+
*.test
18+
19+
# Output of the go coverage tool, specifically when used with LiteIDE
20+
*.out
21+
22+
# Dependency directories (remove the comment below to include it)
23+
# vendor/
24+
25+
# Go workspace file
26+
go.work
27+
28+
### Go Patch ###
29+
/vendor/
30+
/Godeps/
31+
32+
### VisualStudioCode ###
33+
.vscode/*
34+
!.vscode/settings.json
35+
!.vscode/tasks.json
36+
!.vscode/launch.json
37+
!.vscode/extensions.json
38+
!.vscode/*.code-snippets
39+
40+
# Local History for Visual Studio Code
41+
.history/
42+
43+
# Built Visual Studio Code Extensions
44+
*.vsix
45+
46+
### VisualStudioCode Patch ###
47+
# Ignore all local history of files
48+
.history
49+
.ionide
50+
51+
# Support for Project snippet scope
52+
.vscode/*.code-snippets
53+
54+
# Ignore code-workspaces
55+
*.code-workspace
56+
57+
### Windows ###
58+
# Windows thumbnail cache files
59+
Thumbs.db
60+
Thumbs.db:encryptable
61+
ehthumbs.db
62+
ehthumbs_vista.db
63+
64+
# Dump file
65+
*.stackdump
66+
67+
# Folder config file
68+
[Dd]esktop.ini
69+
70+
# Recycle Bin used on file shares
71+
$RECYCLE.BIN/
72+
73+
# Windows Installer files
74+
*.cab
75+
*.msi
76+
*.msix
77+
*.msm
78+
*.msp
79+
80+
# Windows shortcuts
81+
*.lnk
82+
83+
# End of https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,go
84+
85+
# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
86+

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# __go-gin-rest-api__
2+
3+
Golang gin framework REST API with JWT authentication
4+
5+
__URL Endpoints:__
6+
7+
``http://localhost:8080``
8+
9+
| Endpoint | Method |
10+
| ------------- | ------------- |
11+
| ``/[email protected]&password=test`` | __POST__ |
12+
| ``/emi`` | __POST__ |
13+
14+
## Follow the steps to Run App
15+
16+
go mod init github.com/AIRONAX-Developer/go-gin-rest-api
17+
18+
go mod tidy
19+
20+
go mod vendor
21+
22+
go mod run main.go

go.mod

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module github.com/AIRONAX-Developer/go-gin-rest-api
2+
3+
go 1.19
4+
5+
require (
6+
github.com/gin-gonic/gin v1.8.1
7+
github.com/golang-jwt/jwt/v4 v4.4.2
8+
)
9+
10+
require (
11+
github.com/gin-contrib/sse v0.1.0 // indirect
12+
github.com/go-playground/locales v0.14.0 // indirect
13+
github.com/go-playground/universal-translator v0.18.0 // indirect
14+
github.com/go-playground/validator/v10 v10.10.0 // indirect
15+
github.com/goccy/go-json v0.9.7 // indirect
16+
github.com/json-iterator/go v1.1.12 // indirect
17+
github.com/leodido/go-urn v1.2.1 // indirect
18+
github.com/mattn/go-isatty v0.0.14 // indirect
19+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
20+
github.com/modern-go/reflect2 v1.0.2 // indirect
21+
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
22+
github.com/ugorji/go/codec v1.2.7 // indirect
23+
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
24+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
25+
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
26+
golang.org/x/text v0.3.6 // indirect
27+
google.golang.org/protobuf v1.28.0 // indirect
28+
gopkg.in/yaml.v2 v2.4.0 // indirect
29+
)

go.sum

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
6+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
7+
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
8+
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
9+
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
10+
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
11+
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
12+
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
13+
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
14+
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
15+
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
16+
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
17+
github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM=
18+
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
19+
github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQAYs=
20+
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
21+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
22+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
23+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
24+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
25+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
26+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
27+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
28+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
29+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
30+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
31+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
32+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
33+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
34+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
35+
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
36+
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
37+
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
38+
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
39+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
40+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
41+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
42+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
43+
github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU=
44+
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
45+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
46+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
47+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
48+
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
49+
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
50+
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
51+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
52+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
53+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
54+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
55+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
56+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
57+
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
58+
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
59+
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
60+
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
61+
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
62+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
63+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
64+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
65+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
66+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
67+
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU=
68+
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
69+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
70+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
71+
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
72+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
73+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
74+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
75+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
76+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
77+
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
78+
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
79+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
80+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
81+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
82+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
83+
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
84+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
85+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
86+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
87+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
88+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
controller "github.com/AIRONAX-Developer/go-gin-rest-api/src/controller"
6+
service "github.com/AIRONAX-Developer/go-gin-rest-api/src/service"
7+
"math"
8+
"net/http"
9+
10+
"github.com/gin-gonic/gin"
11+
)
12+
13+
func main() {
14+
var loginService service.LoginService = service.StaticLoginService()
15+
var jwtService service.JWTService = service.JWTAuthService()
16+
var loginController controller.LoginController = controller.LoginHandler(loginService, jwtService)
17+
18+
server := gin.New()
19+
20+
fmt.Println("loginService ==>> ", loginService)
21+
fmt.Println("jwtService ==>> ", jwtService)
22+
fmt.Println("loginController ==>> ", loginController)
23+
24+
server.POST("/login", func(ctx *gin.Context) {
25+
token := loginController.Login(ctx)
26+
fmt.Println("token ==>> ", token)
27+
if token != "" {
28+
ctx.JSON(http.StatusOK, gin.H{
29+
"token": token,
30+
})
31+
} else {
32+
ctx.JSON(http.StatusUnauthorized, nil)
33+
}
34+
})
35+
36+
server.POST("/emi", func(ctx *gin.Context) {
37+
// EMI Calculation Formula
38+
/*
39+
E is EMI
40+
41+
P is Principal Loan Amount
42+
43+
r is rate of interest calculated on monthly basis. (i.e., r = Rate of Annual interest/12/100. If rate of interest is 10.5% per annum, then r = 10.5/12/100=0.00875)
44+
45+
n is loan term / tenure / duration in number of months
46+
47+
E = P*r* (1+r)^n/(((1+r)^n)-1)
48+
*/
49+
P := 1000000
50+
r := 10.5 / 12 / 100
51+
n := 120
52+
53+
numerator := math.Pow((1 + r), float64(n))
54+
denominator := (math.Pow((1+r), float64(n)) - 1)
55+
56+
Emi := int(math.Round(float64(P) * r * numerator / denominator))
57+
58+
ctx.JSON(http.StatusOK, gin.H{
59+
"Principal": P,
60+
"InterestRate": r,
61+
"Tenure": n,
62+
"Emi": Emi,
63+
"TotalEmi": (Emi * n),
64+
"TotalInterest": ((Emi * n) - P),
65+
})
66+
})
67+
68+
port := "8080"
69+
server.Run(":" + port)
70+
}

src/LoCred/LoginCredentials.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package LoCred
2+
3+
type LoginCredentialsStruct struct {
4+
Email string `form:"email"`
5+
Password string `form:"password"`
6+
}

src/controller/LoginController.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package controller
2+
3+
import (
4+
"fmt"
5+
locred "github.com/AIRONAX-Developer/go-gin-rest-api/src/LoCred"
6+
service "github.com/AIRONAX-Developer/go-gin-rest-api/src/service"
7+
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
type LoginController interface {
12+
Login(ctx *gin.Context) string
13+
}
14+
15+
type LoginControllerStruct struct {
16+
loginService service.LoginService
17+
jwtService service.JWTService
18+
}
19+
20+
func LoginHandler(loginService service.LoginService, jwtService service.JWTService) LoginController {
21+
return &LoginControllerStruct{
22+
loginService: loginService,
23+
jwtService: jwtService,
24+
}
25+
}
26+
27+
func (controller *LoginControllerStruct) Login(ctx *gin.Context) string {
28+
var credentials locred.LoginCredentialsStruct
29+
err := ctx.ShouldBind(&credentials)
30+
if err != nil {
31+
fmt.Println("ctx.ShouldBind ==>> ", err)
32+
return err.Error()
33+
}
34+
35+
isUserAuthenticated := controller.loginService.LoginUser(credentials.Email, credentials.Password)
36+
if isUserAuthenticated {
37+
return controller.jwtService.GenerateToken(credentials.Email, true)
38+
}
39+
return ""
40+
}

src/middleware/JWTMiddleware.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package middleware
2+
3+
import (
4+
"fmt"
5+
service "github.com/AIRONAX-Developer/go-gin-rest-api/src/service"
6+
"net/http"
7+
8+
"github.com/golang-jwt/jwt/v4"
9+
"github.com/gin-gonic/gin"
10+
)
11+
12+
func AuthorizeJWT() gin.HandlerFunc {
13+
return func(c *gin.Context) {
14+
const BEARER_SCHEMA = "Bearer"
15+
authHeader := c.GetHeader("Authorization")
16+
tokenString := authHeader[len(BEARER_SCHEMA):]
17+
// token, err := service.JWTAuthService().ValidateToken(tokenString)
18+
token, err := service.JWTAuthService().ValidateToken(tokenString)
19+
if token.Valid {
20+
claims := token.Claims.(jwt.MapClaims)
21+
fmt.Println(claims)
22+
} else {
23+
fmt.Println(err)
24+
c.AbortWithStatus(http.StatusUnauthorized)
25+
}
26+
27+
}
28+
}

0 commit comments

Comments
 (0)