You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

279 lines
9.5 KiB

import 'package:camera/camera.dart';
import 'package:teso/Classes/API%20Clasess/TokenHandler.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:teso/Pages/PageWidgets/Login/verificationCode.dart';
import 'package:teso/util/consts.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:teso/Classes/API%20Clasess/Registrar.dart';
import 'package:teso/main_screen.dart';
import 'package:teso/Classes/API%20Clasess/UserAuth.dart';
import 'package:teso/Pages/Sub_Pages/LandingPage/Login.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:teso/providers/user_provider.dart';
import 'package:firebase_auth/firebase_auth.dart';
class Verification extends StatefulWidget {
final Registrar user;
const Verification({Key key, this.user}) : super(key: key);
@override
_VerificationState createState() => _VerificationState(user: this.user);
}
class _VerificationState extends State<Verification> {
List<CameraDescription> connectedCameras;
Registrar user;
_VerificationState({this.user});
TextEditingController code = new TextEditingController();
bool loading = false;
bool error = false;
FirebaseAuth _auth;
@override
void initState() {
availableCameras().then((value) {
connectedCameras = value;
});
super.initState();
}
void verifyAccount() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
loading = true;
});
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
};
int codeEntered = int.parse(code.text.toString());
var register = serverLocation + 'api/activationhandler';
var client = await http.post(Uri.parse(register),
body: json.encode(codeEntered), headers: requestHeaders);
if (client.statusCode == 200) {
UserAuth auth = user.authentication;
var register2 = serverLocation + 'api/tokens';
var client1 = await http.post(Uri.parse(register2),
body: json.encode(auth), headers: requestHeaders);
if (client1.statusCode == 200) {
Map handler = jsonDecode(client1.body);
TokenHandler tokenHandler = TokenHandler.fromJSON(handler);
_auth = FirebaseAuth.instance;
await _auth.signInWithCustomToken(tokenHandler.tokenFirebase);
final QuerySnapshot result = await FirebaseFirestore.instance
.collection('users')
.where('id', isEqualTo: tokenHandler.user.userGUID)
.get();
final List<DocumentSnapshot> documents = result.docs;
if (documents.length == 0) {
// Update data to server if new user
FirebaseFirestore.instance
.collection('users')
.doc(tokenHandler.user.userGUID)
.set({
'nickname': tokenHandler.user.username,
'id': tokenHandler.user.userGUID
});
}
FirebaseFirestore.instance
.collection('users')
.doc(_auth.currentUser.uid)
.update({'deviceToken': auth.deviceToken});
prefs.setString("tokensTeso", "Bearer " + tokenHandler.tokenTeso);
prefs.setString("tokensFirebase", tokenHandler.tokenFirebase);
prefs.setString("accountType", "email");
prefs.setString("id", tokenHandler.user.userGUID);
prefs.setString("currentUser", tokenHandler.user.toString());
prefs.setBool("password", true);
UserProvider userProvider = UserProvider();
userProvider.setUser(tokenHandler.user);
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => MainScreens(
connectedCameras: connectedCameras,
)),
(Route<dynamic> route) => false);
} else {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => LoginPage(
connectedCameras: connectedCameras,
)),
(Route<dynamic> route) => false);
}
} else {
setState(() {
loading = false;
error = true;
});
}
setState(() {
loading = false;
});
}
void reGenerateActivation() async {
setState(() {
loading = true;
});
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
};
var register = serverLocation + 'api/activationgenerator';
await http.post(Uri.parse(register),
body: json.encode(user.user), headers: requestHeaders);
setState(() {
loading = false;
});
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () {
Navigator.of(context).pop();
},
),
title: Text("Account Verification"),
),
body: Container(
height: MediaQuery.of(context).size.height * 0.9,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: new Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(10),
width: double.infinity,
margin: EdgeInsets.only(
bottom: MediaQuery.of(context).size.height * 0.02,
top: MediaQuery.of(context).size.height * 0.02,
),
child: Center(
child: Text(
"Enter the verification code we sent to your email " +
user.user.email +
" to activate your account.",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.grey,
),
),
),
),
SizedBox(
height: 10,
),
inputCode(context, code),
SizedBox(height: MediaQuery.of(context).size.height * 0.001),
Visibility(
visible: loading,
child: Center(
child: CupertinoActivityIndicator(
animating: true,
radius: 15,
),
),
),
Visibility(
visible: error,
child: Container(
margin: EdgeInsets.only(top: 20),
width: MediaQuery.of(context).size.width * 0.9,
child: Center(
child: Text(
"An error occurred while verifying account try again!!",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.red,
),
),
),
),
),
Container(
margin: EdgeInsets.only(top: 20),
width: MediaQuery.of(context).size.width * 0.6,
height: 40.0,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
accentMain,
darkAccent,
],
// stops: [0.1, 0.4, 0.7, 0.8],
),
boxShadow: [
BoxShadow(
color: Colors.grey[500],
offset: Offset(0.0, 1.5),
blurRadius: 1.5,
),
]),
child: Material(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(25.0),
),
),
color: Colors.transparent,
child: InkWell(
onTap: () => verifyAccount(),
child: Center(
child: Text(
"Continue",
style: TextStyle(
fontSize: 18,
color: Colors.white,
),
),
)),
),
),
SizedBox(height: MediaQuery.of(context).size.height * 0.001),
InkWell(
onTap: reGenerateActivation,
child: Container(
margin: EdgeInsets.only(top: 20),
width: MediaQuery.of(context).size.width * 0.9,
child: Center(
child: Text(
"Didn't receive the code ? Click to resend",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.blueAccent,
),
),
),
),
),
],
),
),
));
}
}