-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
210 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters