Skip to content

Commit

Permalink
feat: grpc login and create user
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2004 committed Aug 11, 2024
1 parent 3a75b42 commit ccf5137
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 12 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
You have to select the pb package first, by running this command:

package pb

Then you would be able to show the service of that package:
show service

Next, you select the API service by running:
service xufin
17 changes: 17 additions & 0 deletions gapi/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gapi

import (
db "github.com/arya2004/xyfin/db/sqlc"
"github.com/arya2004/xyfin/pb"
"google.golang.org/protobuf/types/known/timestamppb"
)

func ConvertUser(user db.User) *pb.User {
return &pb.User{
Username: user.Username,
FullName: user.FullName,
Email: user.Email,
PasswordChangedAt: timestamppb.New(user.PasswordChangedAt),
CreatedAt: timestamppb.New(user.CreatedAt),
}
}
48 changes: 48 additions & 0 deletions gapi/rpc_create_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gapi

import (
"context"

db "github.com/arya2004/xyfin/db/sqlc"
"github.com/arya2004/xyfin/pb"
"github.com/arya2004/xyfin/utils"
"github.com/lib/pq"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)


func (server *Server) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {

hashedPassword, err := utils.HashPassword(req.GetPassword())
if err != nil {
return nil, status.Errorf(codes.Internal, "failled to hash -password: %v", err)
}

arg := db.CreateUserParams{
FullName: req.GetFullName(),
Email: req.GetEmail(),
Username: req.GetUsername(),
HashedPassword: hashedPassword,
}

user, err := server.store.CreateUser(ctx, arg)
if err != nil {
if pqErr, ok := err.(*pq.Error); ok {
switch pqErr.Code.Name() {
case "unique_violation":
return nil, status.Errorf(codes.AlreadyExists, "username already exist %v", err)

}
}
return nil, status.Errorf(codes.Internal, "failled to create user: %v", err)

}

resp := &pb.CreateUserResponse{
User: ConvertUser(user),
}

return resp, nil
}

73 changes: 73 additions & 0 deletions gapi/rpc_login_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package gapi

import (
"context"
"database/sql"

db "github.com/arya2004/xyfin/db/sqlc"
"github.com/arya2004/xyfin/pb"
"github.com/arya2004/xyfin/utils"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)



func (server *Server) LoginUser(ctx context.Context, req *pb.LoginUserRequest) (*pb.LoginUserResponse, error) {

user, err := server.store.GetUser(ctx, req.GetUsername())
if err != nil {
if err == sql.ErrNoRows{
return nil, status.Errorf(codes.NotFound, "user not found")
}
return nil, status.Errorf(codes.Internal, "falled to find user")
}

err = utils.CheckPassword(req.Password, user.HashedPassword)
if err != nil {
return nil, status.Errorf(codes.NotFound, "incorrect password")
}

accessToken,accessPayload, err := server.tokenMaker.CreateToken(
user.Username,
server.config.AccessTokenDuration,
)

if err != nil {
return nil, status.Errorf(codes.Internal, "failled to create access token")
}

refreshToken,refreshPayload, err := server.tokenMaker.CreateToken(
user.Username,
server.config.RefreshTokenDuration,
)

if err != nil {
return nil, status.Errorf(codes.Internal, "faliled to create refresh token")
}

session, err := server.store.CreateSession(ctx, db.CreateSessionParams{
ID: refreshPayload.ID,
Username: user.Username,
RefreshToken: refreshToken,
UserAgent: "ctx.Request.UserAgent()",
ClientIp: "ctx.ClientIP()",
IsBlocked: false,
ExpiresAt: refreshPayload.ExpiredAt,
})

if err != nil {
return nil, status.Errorf(codes.Internal, "failled to create session")
}

rsp := &pb.LoginUserResponse{
User: ConvertUser(user),
SessionId: session.ID.String(),
AccessToken: accessToken,
RefreshToken: refreshToken,
AccessTokenExpiresAt: timestamppb.New(accessPayload.ExpiredAt),
RefreshTokenExpiresAt: timestamppb.New(refreshPayload.ExpiredAt),
}
return rsp, nil
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ func main() {
}

func runGrpcServer(config utils.Configuration, store db.Store) {
grpcServer := grpc.NewServer()

server, err := gapi.NewServer(config, store)
if err != nil {
log.Fatal("cannot create server", err)
}
grpcServer := grpc.NewServer()
pb.RegisterXyfinServer(grpcServer, server)
reflection.Register(grpcServer)

Expand Down
31 changes: 21 additions & 10 deletions pb/service_xyfin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions pb/service_xyfin_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion proto/service_xyfin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ syntax = "proto3";
package pb;

import "rpc_create_user.proto";
import "rpc_login_user.proto";

option go_package = "github.com/arya2004/xyfin/pb";

service Xyfin {
rpc CreateUser (CreateUserRequest) returns (CreateUserResponse){}

rpc LoginUser (LoginUserRequest) returns (LoginUserResponse) {}
}

0 comments on commit ccf5137

Please sign in to comment.