Skip to content
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

#466 tokenization for mobile app #493

Merged
merged 1 commit into from
Nov 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion ludos/mobile/lib/create_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ Widget getbox(String hintText, TextEditingController controller,
}

class CreateGamePage extends StatefulWidget {
const CreateGamePage({Key? key}) : super(key: key);
final String? token;
const CreateGamePage({Key? key, required this.token}) : super(key: key);

@override
State<CreateGamePage> createState() => _CreateGamePageState();
Expand Down Expand Up @@ -385,6 +386,7 @@ class _CreateGamePageState extends State<CreateGamePage> {
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => CreateGamePageSecond(
token: widget.token,
title: titleController.text,
coverLink: coverLinkController.text,
gameBio: gameBioController.text,
Expand Down
7 changes: 5 additions & 2 deletions ludos/mobile/lib/create_game_second.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ String formatDateTime(DateTime dateTime) {
}

class CreateGamePageSecond extends StatefulWidget {
final String? token;
final String title;
final String coverLink;
final String gameBio;
Expand All @@ -94,6 +95,7 @@ class CreateGamePageSecond extends StatefulWidget {

const CreateGamePageSecond(
{Key? key,
required this.token,
required this.title,
required this.coverLink,
required this.gameBio,
Expand Down Expand Up @@ -286,6 +288,7 @@ class _CreateGamePageStateSecond extends State<CreateGamePageSecond> {
),
onPressed: () async {
http.Response token = await APIService().createGame(
widget.token,
widget.title,
widget.coverLink,
systemRequirementsController.text,
Expand Down Expand Up @@ -338,7 +341,7 @@ class _CreateGamePageStateSecond extends State<CreateGamePageSecond> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => GamesPage()),
builder: (context) => GamesPage(token: widget.token)),
);
},
),
Expand All @@ -348,7 +351,7 @@ class _CreateGamePageStateSecond extends State<CreateGamePageSecond> {
.then((reason) => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => GamesPage()),
builder: (context) => GamesPage(token: widget.token)),
));
} else {
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
12 changes: 7 additions & 5 deletions ludos/mobile/lib/games_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import 'helper/APIService.dart';
import 'create_game.dart';

class GamesPage extends StatefulWidget {
const GamesPage({Key? key}) : super(key: key);
final String? token;
const GamesPage({Key? key, required this.token}) : super(key: key);

@override
State<GamesPage> createState() => _GamesPageState();
Expand All @@ -20,11 +21,12 @@ class _GamesPageState extends State<GamesPage> {
@override
void initState() {
super.initState();
games = fetchData();
games = fetchData(widget.token);
}

Future<List<GameSummary>> fetchData() async {
final response = await APIService().listGames();
Future<List<GameSummary>> fetchData(String? token) async {
//final userProvider = Provider.of<UserProvider>(context, listen: false);
final response = await APIService().listGames(token);
try {
//print(json.decode(response.body));
if (response.statusCode == 200) {
Expand Down Expand Up @@ -120,7 +122,7 @@ class _GamesPageState extends State<GamesPage> {
),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const CreateGamePage(),
builder: (context) => CreateGamePage(token: widget.token),
));
}, child: const Text(
'Create Game',
Expand Down
10 changes: 5 additions & 5 deletions ludos/mobile/lib/helper/APIService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class APIService {
return response;
}

Future<http.Response> createGame(String title, String coverLink,
Future<http.Response> createGame(String? authToken, String title, String coverLink,
String systemRequirements, List<String> predecessors, List<String> successors,
String gameGuide, String gameStory, List<String> platforms, String ageRestriction,
String gameBio, List<String> tags, String releaseDate, String developer,
Expand All @@ -62,7 +62,7 @@ class APIService {
'publisher': publisher,
'trivia': trivia,
});
final response = await http.post(uri, body: body, headers: {'content-type': "application/json"});
final response = await http.post(uri, body: body, headers: {'content-type': "application/json", 'Authorization': 'Bearer $authToken'});

return response;
}
Expand All @@ -89,9 +89,9 @@ class APIService {
return response;
}

Future<http.Response> listGames() async {
var uri = Uri.parse("$baseURL/game?limit=10");
final response = await http.get(uri, headers: {'content-type': "application/json"});
Future<http.Response> listGames(String? authToken) async {
var uri = Uri.parse("$baseURL/game?limit=20");
final response = await http.get(uri, headers: {'content-type': "application/json", 'Authorization': 'Bearer $authToken'});

return response;
}
Expand Down
2 changes: 1 addition & 1 deletion ludos/mobile/lib/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class LoginPageState extends State<LoginPage> {
onPressed: () async {
(String?, int) token = await APIService()
.login(emailController.text, passwordController.text);
print(token);
//print(token);
if (token.$2 == 200) {
Provider.of<UserProvider>(context, listen: false)
.setLoggedIn(true, emailController.text, token.$1);
Expand Down
47 changes: 45 additions & 2 deletions ludos/mobile/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,53 @@ class Home extends StatelessWidget {
builder: (context) => const CreateGamePage(key: null,),
));
*/
if(userProvider.isLoggedIn){
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const GamesPage(),
builder: (context) => GamesPage(token: userProvider.token),
));

}
else{
ScaffoldMessenger.of(context)
.showSnackBar(
SnackBar(
content: const Row(
children: [
Icon(
Icons.check_circle_outline,
color: MyColors.blue,
),
SizedBox(width: 8),
Expanded(
child: Text(
'Please log in to view games',
style: TextStyle(
color: MyColors.blue,
fontSize: 16,
),
),
),
],
),
backgroundColor: MyColors.blue2,
duration: const Duration(seconds: 5),
action: SnackBarAction(
label: 'Log In',
textColor: MyColors.blue,
onPressed: () {
ScaffoldMessenger.of(context)
.hideCurrentSnackBar();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LoginPage()),
);
},
),
),
)
.closed
.then((reason) => {});
}
},
icon: const Icon(Icons.games)),
IconButton(
Expand Down