Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
22_23-J 21
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
22_23-J 21
22_23-J 21
Commits
3be36692
Commit
3be36692
authored
May 10, 2023
by
Rathnayake K.U.R.S.D
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added auth service
parent
a6a98093
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
0 deletions
+82
-0
FrontEnd/lib/services/auth_service.dart
FrontEnd/lib/services/auth_service.dart
+82
-0
No files found.
FrontEnd/lib/services/auth_service.dart
0 → 100644
View file @
3be36692
import
'package:firebase_auth/firebase_auth.dart'
;
import
'package:flutter/material.dart'
;
import
'package:govimithura/models/auth_model.dart'
;
import
'package:govimithura/models/error_model.dart'
;
class
AuthService
{
static
final
FirebaseAuth
_auth
=
FirebaseAuth
.
instance
;
Future
<
bool
>
login
(
AuthModel
authModel
)
async
{
try
{
UserCredential
userCred
=
await
_auth
.
signInWithEmailAndPassword
(
email:
authModel
.
email
,
password:
authModel
.
password
)
.
then
((
value
)
=>
value
);
return
userCred
.
user
?.
uid
!=
null
;
}
on
FirebaseAuthException
catch
(
e
)
{
if
(
e
.
code
==
'user-not-found'
)
{
ErrorModel
.
errorMessage
=
'No user found for this email.'
;
}
else
if
(
e
.
code
==
'wrong-password'
)
{
ErrorModel
.
errorMessage
=
'Wrong password provided for this user.'
;
}
else
{
ErrorModel
.
errorMessage
=
e
.
message
??
''
;
}
}
on
Exception
catch
(
e
)
{
ErrorModel
.
errorMessage
=
e
.
toString
();
}
return
false
;
}
Future
<
AuthModel
?>
register
(
AuthModel
authModel
)
async
{
try
{
await
_auth
.
createUserWithEmailAndPassword
(
email:
authModel
.
email
,
password:
authModel
.
password
)
.
then
((
value
)
=>
{
debugPrint
(
"User registered
${value.user!.uid}
"
),
authModel
.
uid
=
value
.
user
!.
uid
,
});
return
authModel
;
}
on
FirebaseAuthException
catch
(
e
)
{
if
(
e
.
code
==
'email-already-in-use'
)
{
ErrorModel
.
errorMessage
=
'The account already exists for this email.'
;
}
else
if
(
e
.
code
==
'invalid-email'
)
{
ErrorModel
.
errorMessage
=
'Invalid email address.'
;
}
else
{
ErrorModel
.
errorMessage
=
e
.
message
??
''
;
}
return
null
;
}
on
Exception
catch
(
e
)
{
ErrorModel
.
errorMessage
=
e
.
toString
();
return
null
;
}
}
bool
isLoggedIn
()
{
_auth
.
userChanges
().
listen
((
User
?
user
)
{
if
(
user
==
null
)
{
debugPrint
(
'User is currently signed out!'
);
}
else
{
debugPrint
(
'User is signed in!'
);
}
});
return
_auth
.
currentUser
!=
null
;
}
Future
<
void
>
signOut
()
async
{
await
_auth
.
signOut
();
}
static
Future
<
void
>?
forgetPassword
(
String
email
)
async
{
try
{
return
await
_auth
.
sendPasswordResetEmail
(
email:
email
);
}
on
FirebaseAuthException
catch
(
e
)
{
ErrorModel
.
errorMessage
=
e
.
message
??
''
;
return
;
}
on
Exception
catch
(
e
)
{
ErrorModel
.
errorMessage
=
e
.
toString
();
return
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment