Keep in mind that this is a demo of how to authenticate using jwt, we don't cover any issues such as sql injection, you can easily produce it to hack the server.
.
├── cmd # Main applications.
├── config # Configuration file templates or default configs.
├── database # Config for database.
├── environment # Config for environment variables.
├── infrastructure # Config for http server and worker jobs.
├── docs # Swagger API.
├── internal # Private application and library code.
├── app
├── jwt-for-authentication
├── controllers # REST controllers.
├── dto # Data transfer object.
├── middlware # Middleware of REST server.
├── models # Model of RDBMS or NoSQL.
├── repositories # Repositories layer.
├── routers # Routing.
├── services # Business services.
├── utils # Utility.
├── script # Scripts to perform various build or test
└── README.md
Nogopy uses a number of open source projects to work properly:
- Gorm - The fantastic ORM library for Golang
- Gin - Gin is a web framework written in Go
- Logrus - Logrus is a structured logger for Go
Golang-template requires Golang v1.16 to run.
Install the dependencies and devDependencies and start the server.
export PATH=$PATH:$HOME/go/bin
go mod init github.com/nogopy/jwt-for-authentication
go mod tidy
go get github.com/codegangsta/gin
cd /usr/local/mysql/bin
./mysql -u root -p then enter password
create database login
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(200) NOT NULL,
`password` varchar(200) NOT NULL,
`created_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
# Don't add passphrase
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub
./script/run.sh
Verify the deployment by navigating to your server address in your preferred browser.
127.0.0.1:8080
What is jwt and how it works https://jwt.io/introduction
Source: https://www.freecodecamp.org/news/how-to-setup-jwt-authorization-and-authentication-in-spring/