Skip to content

work on signin, signup, appwrite auth, consts, etc #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/appwrite_service/appwrite_constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AppWriteConst {
static const String APPWRITE_PROJECT_ID = "64803e0044c9826d779b";
static const String APPWRITE_ENDPOINT = "https://cloud.appwrite.io/v1";
static const String APPWRITE_DATABASE_ID = "";
static const String COLLECTION_MESSAGES = "";
}
Empty file.
127 changes: 127 additions & 0 deletions lib/appwrite_service/auth_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import 'package:appwrite/appwrite.dart';
import 'package:appwrite/models.dart';
import 'package:flutter/material.dart';

import 'appwrite_constants.dart';

// class AuthService {
// final client = Client()
// .setEndpoint(AppWriteConst.APPWRITE_ENDPOINT) // Your API Endpoint
// .setProject(AppWriteConst.APPWRITE_PROJECT_ID); // Your project ID

// static signInWithEmailAndPassword() {}
// static signInWithGitHub() {}
// static signInWithGoogle() {}
// }

enum AuthStatus {
uninitialized,
authenticated,
unauthenticated,
}

class AuthService extends ChangeNotifier {
Client client = Client();
late final Account account;

late User _currentUser;

AuthStatus _status = AuthStatus.uninitialized;

// Getter methods
User get currentUser => _currentUser;
AuthStatus get status => _status;
String? get username => _currentUser.name;
String? get email => _currentUser.email;
String? get userid => _currentUser.$id;

// Constructor
AuthService() {
init();
loadUser();
}

// Initialize the Appwrite client
init() {
client.setEndpoint(AppWriteConst.APPWRITE_ENDPOINT).setProject(AppWriteConst.APPWRITE_PROJECT_ID).setSelfSigned();
account = Account(client);
}

loadUser() async {
try {
final user = await account.get();
_status = AuthStatus.authenticated;
_currentUser = user;
} catch (e) {
_status = AuthStatus.unauthenticated;
} finally {
notifyListeners();
}
}

Future<User> createUser({required String email, required String password}) async {
notifyListeners();

try {
final user = await account.create(userId: ID.unique(), email: email, password: password, name: 'Sam Alt');
return user;
} finally {
notifyListeners();
}
}

createVerification() async {
notifyListeners();

try {
Token result = await account.createVerification(
url: 'http://localhost:50423/#/',
);
print(result.secret);
// return result;
} finally {
notifyListeners();
}
}

Future<Session> createEmailSession({required String email, required String password}) async {
notifyListeners();

try {
final session = await account.createEmailSession(email: email, password: password);
_currentUser = await account.get();
_status = AuthStatus.authenticated;
return session;
} finally {
notifyListeners();
}
}

signInWithProvider({required String provider}) async {
try {
final session = await account.createOAuth2Session(provider: provider);
_currentUser = await account.get();
_status = AuthStatus.authenticated;
return session;
} finally {
notifyListeners();
}
}

signOut() async {
try {
await account.deleteSession(sessionId: 'current');
_status = AuthStatus.unauthenticated;
} finally {
notifyListeners();
}
}

Future<Preferences> getUserPreferences() async {
return await account.getPrefs();
}

updatePreferences({required String bio}) async {
return account.updatePrefs(prefs: {'bio': bio});
}
}
1 change: 1 addition & 0 deletions lib/appwrite_service/storage_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class StorageService {}
4 changes: 4 additions & 0 deletions lib/constants/app_constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'package:flutter/material.dart';

final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
5 changes: 4 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:help_me_design/appwrite_service/auth_service.dart';
import 'package:help_me_design/providers/component_tab_provider/component_tab_provider.dart';
import 'package:help_me_design/theme/my_theme.dart';
import 'package:help_me_design/views/screens/onboarding_screens/signin_screen.dart';
Expand All @@ -7,6 +8,7 @@ import 'package:help_me_design/views/screens/onboarding_screens/signup_screen.da
import 'package:help_me_design/views/welcome_screen.dart';
import 'package:provider/provider.dart';

import 'constants/app_constants.dart';
import 'providers/snippet_tab_provider.dart';

void main() {
Expand All @@ -23,14 +25,15 @@ class MyApp extends StatelessWidget {
ChangeNotifierProvider<ThemeManager>(create: (_) => ThemeManager()),
ChangeNotifierProvider<SnippetTabProvider>(create: (_) => SnippetTabProvider()),
ChangeNotifierProvider<ComponentTabProvider>(create: (_) => ComponentTabProvider()),
ChangeNotifierProvider<AuthService>(create: (_) => AuthService()),
],
child: Consumer<ThemeManager>(builder: (context, value, snapshot) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Help Me Design',
darkTheme: darkTheme,
theme: lightTheme,

scaffoldMessengerKey: scaffoldMessengerKey,
scrollBehavior: const ScrollBehavior().copyWith(scrollbars: false),
themeMode: Provider.of<ThemeManager>(context).getThemeMode,
// home: const MyHomePage(),
Expand Down
21 changes: 21 additions & 0 deletions lib/utility/email_validation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// `isEmailValid` can be used for check the Email is valid or not
bool isEmailValid(String userEmail) {
RegExp validateEmail = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+");

if (validateEmail.hasMatch(userEmail) == true) {
return true;
} else {
return false;
}
}

/// `isPasswordValid` can be used for check the password is valid or not
bool isPasswordValid(userPassword) {
RegExp validatePassword = RegExp(r'^(?=.*?[a-zA-Z])(?=.*?[0-9])(?=.*?[!@#\$&*~<>]).{8,}$');

if (validatePassword.hasMatch(userPassword) == true) {
return true;
} else {
return false;
}
}
16 changes: 16 additions & 0 deletions lib/utility/my_message.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Msg {
static const login = "login Successfully";
static const logout = "logout Successfully";
static const completePaymentProcess = "Please complete your payment process";
static const planActivated = "Successfully Activated your plan";
static const provideAllFiled = "Please all provide valid field";
static const providePassword = "Please provide password field";
static const select = "Please select some value field";
static const invalidCredentials = "invalid credentials ";
static const notSemPassword = "Your confirm password is invalid";
static const somethingWrong = "Something went wrong";
static const provideOtp = "Please provide a otp";
static const apiProblem = "Something went wrong";
static const resultNotFound = "Sorry result not found";
static const selectGenres = "Please select atlas 3 genres";
}
48 changes: 48 additions & 0 deletions lib/utility/utility_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:flutter/material.dart';
import 'package:help_me_design/theme/my_colors.dart';

import '../constants/app_constants.dart';

class UtilityHelper {
static toastMessage(myMessage) {
SnackBar snackBar = SnackBar(
// shape: RoundedRectangleBorder(
// // borderRadius: BorderRadius.circular(100),
// ),
elevation: 0,
// behavior: SnackBarBehavior.floating,
width: 404,
behavior: SnackBarBehavior.floating,

duration: const Duration(milliseconds: 1500),
backgroundColor: Colors.transparent,
dismissDirection: DismissDirection.horizontal,
content: Material(
borderRadius: BorderRadius.circular(4),
color: DesignSystemColors.secondaryColorDark,
elevation: 4,
child: Container(
height: 54,
padding: const EdgeInsets.only(left: 18, right: 18),
alignment: Alignment.centerLeft,
child: Text(
myMessage,
maxLines: 1,
style: const TextStyle(
overflow: TextOverflow.ellipsis,
color: Colors.white,
fontSize: 14,
fontWeight: FontWeight.w500,
letterSpacing: -0.1,
),
),
),
),
);
if (myMessage != 'N/A' && myMessage != '') {
scaffoldMessengerKey.currentState?.showSnackBar(snackBar);
} else {
// do something when message is empty but don't show snackBar
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:typed_data';
import 'package:appwrite/appwrite.dart';
import 'package:favicon/favicon.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:help_me_design/appwrite_service/auth_service.dart';
import 'package:help_me_design/theme/my_design_system.dart';
import 'package:help_me_design/theme/my_theme.dart';
import 'package:help_me_design/views/screens/onboarding_screens/signin_signup_screen/widgets/signup_view.dart';
Expand Down Expand Up @@ -37,10 +38,22 @@ class _SignInSignUpScreenState extends State<SignInSignUpScreen> {

final PageController _pageController = PageController();

@override
void initState() {
// TODO: implement initState
super.initState();
var authService = Provider.of<AuthService>(context, listen: false);

if (authService.status == AuthStatus.authenticated) {
log("message");
}
}

@override
Widget build(BuildContext context) {
var themeData = Theme.of(context);
var themeMangerProvider = Provider.of<ThemeManager>(context, listen: false);

return Scaffold(
body: SafeArea(
child: Container(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:appwrite/appwrite.dart';
import 'package:appwrite/models.dart';
import 'package:flutter/material.dart';
import 'package:help_me_design/appwrite_service/auth_service.dart';
import 'package:help_me_design/theme/my_design_system.dart';
import 'package:help_me_design/theme/my_theme.dart';
import 'package:help_me_design/views/widgets/button_tap_effect.dart';
Expand All @@ -9,15 +11,20 @@ import 'package:help_me_design/views/widgets/form_widgets/buttons/button_with_ti
import 'package:help_me_design/views/widgets/form_widgets/buttons/simple_button.dart';
import 'package:help_me_design/views/widgets/form_widgets/input_fields/email_input_field.dart';
import 'package:help_me_design/views/widgets/form_widgets/input_fields/password_input_field.dart';
import 'package:provider/provider.dart';

class SignInView extends StatelessWidget {
const SignInView({Key? key, required this.onTapSignUp}) : super(key: key);
SignInView({Key? key, required this.onTapSignUp}) : super(key: key);

final VoidCallback onTapSignUp;

TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();

@override
Widget build(BuildContext context) {
var themeData = Theme.of(context);
var authService = Provider.of<AuthService>(context);
return Container(
// height: 500,
width: 374,
Expand Down Expand Up @@ -60,12 +67,12 @@ class SignInView extends StatelessWidget {
// Text('Email*', style: themeData.textTheme.titleSmall),
// SizedBox(height: MySpaceSystem.spaceX1),
EmailInputField(
emailEditingController: TextEditingController(),
emailEditingController: emailController,
),
SizedBox(height: MySpaceSystem.spaceX2),
PasswordInputField(
isNeedVisibilityButton: true,
passwordEditingController: TextEditingController(),
passwordEditingController: passwordController,
),
SizedBox(height: MySpaceSystem.spaceX2),
Row(
Expand All @@ -83,8 +90,14 @@ class SignInView extends StatelessWidget {
SizedBox(height: MySpaceSystem.spaceX3),
SimpleButton(
buttonTitle: "Sign In",
onTap: () {
print("object");
onTap: () async {
// print("object");
User user = await authService.createUser(
email: emailController.text,
password: passwordController.text,
);

print(user.email);
},
),

Expand All @@ -96,16 +109,19 @@ class SignInView extends StatelessWidget {
children: [
Expanded(
child: ButtonWithTitleAndIcon(
onTap: () {},
onTap: () {
authService.createVerification();
},
buttonTitle: "Google",
icon: Image.asset("assets/images/google-icon.png"),
),
),
SizedBox(width: MySpaceSystem.spaceX2),
Expanded(
child: ButtonWithTitleAndIcon(
onTap: () {
signInGithub(context);
onTap: () async {
// signInGithub(context);
authService.createEmailSession(email: emailController.text, password: passwordController.text);
},
buttonTitle: "Github",
icon: Image.asset("assets/images/github-icon-light.png"),
Expand Down
Loading