diff --git a/example/.gitignore b/example/.gitignore index dce0393..9e45942 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -75,3 +75,5 @@ integration_test/screenshots/ # Firebase .firebase/ +# Config +config/local.json \ No newline at end of file diff --git a/example/lib/src/feature/authentication/data/authentication_repository.dart b/example/lib/src/feature/authentication/data/authentication_repository.dart index 3366be0..c5ae56f 100644 --- a/example/lib/src/feature/authentication/data/authentication_repository.dart +++ b/example/lib/src/feature/authentication/data/authentication_repository.dart @@ -1,5 +1,9 @@ import 'dart:async'; +import 'dart:convert'; +import 'package:convert/convert.dart'; +import 'package:crypto/crypto.dart'; +import 'package:spinify/spinify.dart'; import 'package:spinifyapp/src/feature/authentication/model/sign_in_data.dart'; import 'package:spinifyapp/src/feature/authentication/model/user.dart'; @@ -25,7 +29,31 @@ class AuthenticationRepositoryImpl implements IAuthenticationRepository { Future getToken() async { switch (_user) { case AuthenticatedUser user: - return user.token; + final AuthenticatedUser( + :String username, + :String token, + :String channel, + :String? secret + ) = user; + final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; + String encodeChannel(String secret) => '$channel' + '#' + '${hex.encode(utf8.encoder.fuse(sha256).convert(secret).bytes)}'; + SpinifyJWT jwt = SpinifyJWT( + sub: username, + exp: now + (24 * 60 * 60), + iat: now, + info: { + 'username': username, + }, + channels: [ + switch (secret) { + String secret => encodeChannel(secret), + null => channel, + } + ], + ); + return jwt.encode(token); case UnauthenticatedUser _: throw Exception('User is not authenticated'); } diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 6da43d5..802f9dc 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -55,6 +55,8 @@ dependencies: path: any platform_info: ^4.0.2 win32: ^5.0.6 + crypto: ^3.0.3 + convert: ^3.1.1 # Desktop window_manager: ^0.3.5