Skip to content

Commit

Permalink
api testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash-Khattar committed Dec 14, 2023
1 parent 63141a3 commit 0d0878e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class _MyAppState extends State<MyApp> {
routes: {
AuthScreen.routeName: (context) => AuthScreen(),
NavScreen.routeName: (context) => NavScreen(),
WelcomeScreen.routeName: (context) => WelcomeScreen(),
// YogGuru.routeName: (context) => YogGuru(),
// Camera.routeName: (context) => Camera(),
// SpecificNeeds.routeName: (context) => SpecificNeeds(),
Expand Down
24 changes: 20 additions & 4 deletions lib/screens/userprofile/user_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:yogzen/global/color.dart';
import 'package:yogzen/providers/user_provider.dart';
import 'package:yogzen/screens/welcome/welcome_screen.dart';
import 'package:yogzen/services/auth_services.dart';

class UserProfileScreen extends StatefulWidget {
const UserProfileScreen({super.key});
Expand All @@ -15,9 +17,23 @@ class _UserProfileScreenState extends State<UserProfileScreen> {
Widget build(BuildContext context) {
final user = Provider.of<UserProvider>(context).user;
return Scaffold(
backgroundColor: klightBlue,
body: Center(
child: Text("User Profile of ${user.name}"),
));
appBar: AppBar(
title: Text("User Profile"),
actions: [
IconButton(
onPressed: () {
AuthServices().clearSharedPrefs();
Navigator.pushNamedAndRemoveUntil(
context, WelcomeScreen.routeName, (route) => false);
},
icon: Icon(Icons.logout),
),
],
),
backgroundColor: klightBlue,
body: Center(
child: Text("User Profile of ${user.name}"),
),
);
}
}
1 change: 1 addition & 0 deletions lib/screens/welcome/welcome_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:yogzen/global/color.dart';

class WelcomeScreen extends StatefulWidget {
const WelcomeScreen({super.key});
static const routeName = '/welcome';

@override
State<WelcomeScreen> createState() => _WelcomeScreenState();
Expand Down
2 changes: 1 addition & 1 deletion lib/services/auth_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class AuthServices {
print(response.body);
print(response.statusCode);
} catch (e) {
showSnackBar(context: context, text: e.toString());
print(e.toString());
}
}

Expand Down
24 changes: 18 additions & 6 deletions server/controller/userController.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,25 @@ func VerifyPassword(userPassword string, providedPassword string) (bool, string)
// signup
func Signup() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("1")
var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second)
defer cancel()
var user model.User

fmt.Println("2")
//binding user data from request to user struct
if err := c.BindJSON(&user); err != nil {

c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fmt.Println("3")
//apply validation on user data
validationErr := validate.Struct(user)
if validationErr != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": validationErr.Error()})
return
}
fmt.Println("4")
//check if user already exists
count, err := userCollection.CountDocuments(ctx, bson.M{"email": user.Email})
// defer cancel()
Expand All @@ -70,24 +73,27 @@ func Signup() gin.HandlerFunc {
c.JSON(http.StatusInternalServerError, gin.H{"error": "error while checking email"})
return
}
fmt.Println("5")
password := HashPassword(*user.Password)
user.Password = &password
if count > 0 {
c.JSON(http.StatusInternalServerError, gin.H{"error": "user alraedy exists"})
return
}

fmt.Println("6")
//creating tokens and ids
user.CreatedAt, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
user.UpdatedAt, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
user.ID = primitive.NewObjectID()
user.UserId = user.ID.Hex()
fmt.Println("7")
token, refreshToken, err := helper.GenerateAllToken(*user.Email, *user.Name, *user.UserType, user.UserId)
if err != nil {
log.Panic(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "token error"})
return
}
fmt.Println("8")
user.Token = &token
user.RefreshToken = &refreshToken

Expand All @@ -98,13 +104,15 @@ func Signup() gin.HandlerFunc {
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
fmt.Println("9")
c.JSON(http.StatusOK, resultInsertedNumber)

fmt.Println("10")
}
}

func Login() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("1")
var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second)
defer cancel()
var user model.User
Expand All @@ -113,38 +121,42 @@ func Login() gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

fmt.Println("2")
// finding if user exists
err := userCollection.FindOne(ctx, bson.M{"email": user.Email}).Decode(&foundUser)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "email or password is incorrect"})
return
}

fmt.Println("3")
passwordIsValid, msg := VerifyPassword(*user.Password, *foundUser.Password)
if !passwordIsValid {
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
fmt.Println("4")
if foundUser.Email == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "user not found"})
return
}
fmt.Println("5")
token, refreshToken, err := helper.GenerateAllToken(*foundUser.Email, *foundUser.Name, *foundUser.UserType, foundUser.UserId)
if err != nil {
log.Panic(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "token error"})
return
}
fmt.Println("6")
helper.UpdateAllToken(token, refreshToken, foundUser.UserId)
err = userCollection.FindOne(ctx, bson.M{"userid": foundUser.UserId}).Decode(&foundUser)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
fmt.Println("7")
c.JSON(http.StatusOK, foundUser)

fmt.Println("8")
}
}

Expand Down
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
)

require (
github.com/aws/aws-lambda-go v1.41.0 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/aws/aws-lambda-go v1.41.0 h1:l/5fyVb6Ud9uYd411xdHZzSf2n86TakxzpvIoz7l+3Y=
github.com/aws/aws-lambda-go v1.41.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
Expand Down
2 changes: 1 addition & 1 deletion test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:yogzen/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpWidget( MyApp());

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Expand Down

0 comments on commit 0d0878e

Please sign in to comment.