Commit 49f4ed54 authored by Marius Mauritz's avatar Marius Mauritz
Browse files

es gibt token im frontend

@todo nutzen für kommunikation mit mm
anzugeben im header
parent 606ada52
Pipeline #6317 passed with stage
in 3 minutes and 44 seconds
......@@ -27,7 +27,7 @@ class DashboardComponent implements OnInit, OnActivate {
@override
void onActivate(RouterState previous, RouterState current) async {
if (!LoginComponent.loggedIn) {
_router.navigate('/login');
await _router.navigate('/login');
}
else{
joke = await _dashboardService.getJoke();
......
......@@ -27,7 +27,7 @@ class DayviewComponent implements OnActivate {
@override
void onActivate(_, RouterState current) async {
if (!LoginComponent.loggedIn) {
_router.navigate('/login');
await _router.navigate('/login');
} else {
this.day = Day(
getYear(current.parameters),
......
......@@ -3,6 +3,7 @@ import 'package:angular_forms/angular_forms.dart';
import 'package:angular_router/angular_router.dart';
import 'package:demo/src/view/components/mock_users.dart';
import 'package:demo/src/view/services/person_service.dart';
import 'package:demo/src/view/services/register_service.dart';
import '../../model/person.dart';
import '../main_component.dart';
......@@ -16,10 +17,11 @@ import '../main_component.dart';
class LoginComponent implements OnInit, OnActivate {
LoginComponent(this._router, this._userService);
LoginComponent(this._router, this._userService,this._registerService);
final UserService _userService;
final RegisterService _registerService;
final Router _router;
bool loginFailure = false;
......@@ -32,7 +34,13 @@ class LoginComponent implements OnInit, OnActivate {
//
Future<void> login() async {
await _userService.login(username,password);
loggedIn = true;
AppComponent.showButtons = true;
//await _registerService.login(username,password);
await _router.navigate('/dashboard');
}
......@@ -60,7 +68,7 @@ class LoginComponent implements OnInit, OnActivate {
@override
void onActivate(RouterState previous, RouterState current) {
if (loggedIn) {
if(loggedIn){
_router.navigate('/calendar');
}
}
......
......@@ -55,6 +55,7 @@ class Routes {
static final RouteDefinition login = RouteDefinition(
routePath: RoutePaths.login,
component: login_template.LoginComponentNgFactory as ComponentFactory,
useAsDefault: true,
);
static final RouteDefinition contacts = RouteDefinition(
routePath: RoutePaths.contacts,
......@@ -79,7 +80,7 @@ class Routes {
login,
RouteDefinition.redirect(
path: '',
redirectTo: RoutePaths.dashboard.toUrl(),
redirectTo: RoutePaths.login.toUrl(),
),
];
}
......@@ -10,7 +10,7 @@ class UserService {
UserService(this._http);
static final _headers = {'Content-Type': 'application/json'};
static const _userUrl = host+'/user'; // URL to web API
static const _userUrl = host + '/user'; // URL to web API
final Client _http;
///Liest die Daten aus einer Response
......@@ -41,31 +41,14 @@ class UserService {
}
Future<User> get(String contactCode) async {
final Response response = await _http.get('$_userUrl/$contactCode') as Response;
final Response response =
await _http.get('$_userUrl/$contactCode') as Response;
return User.fromJson(_extractData(response) as Map<String, dynamic>);
}
Future login(String username, String password) async {
// Must include http package in your pubspec.yaml
const clientID = "com.calendar.app";
final body = "username=$username&password=$password&grant_type=password";
// Note the trailing colon (:) after the clientID.
// A client identifier secret would follow this, but there is no secret, so it is the empty string.
final clientCredentials = Base64Encoder().convert("$clientID:".codeUnits);
final response = await _http.post(
"https://stablekernel.com/auth/token",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic $clientCredentials"
},
body: body);
window.localStorage.addAll({"access_token": response["access_token"]as String,"token_type":response["token_type"]as String,"expires_in": response["expires_in"] as String});
}
/*Future<User> getLoginEmail(String password, String email) async {
/*Future<User> getLoginEmail(String password, String email) async {
final Response response = await _http.get('$_userUrl/$password/$email');
return User.fromJson(_extractData(response) as Map<String, dynamic>);
}
......@@ -75,7 +58,7 @@ class UserService {
return User.fromJson(_extractData(response) as Map<String, dynamic>);
}*/
/*///Updatet einen bereits existierenden User
/*///Updatet einen bereits existierenden User
Future<User> update(User user) async {
for (var mockUser in mockUsers) {
if (mockUser.contactCode == user.contactCode) {
......
......@@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:demo/src/view/main_component.dart';
import 'package:http/http.dart';
import 'package:demo/src/model/person.dart';
import 'dart:html';
class RegisterService {
// request to middlemand
......@@ -26,9 +27,33 @@ class RegisterService {
final response = await _http.post(_userUrl,
headers: _headers, body: json.encode(user.toJson()));
return User.fromJson(
_extractData(response) as Map<String, dynamic>);
_extractData(response as Response) as Map<String, dynamic>);
} catch (e) {
throw _handleError(e);
}
}
Future login(String username, String password) async {
// Must include http package in your pubspec.yaml
const clientID = "com.calendar.app";
final body = "username=$username&password=$password&grant_type=password";
// Note the trailing colon (:) after the clientID.
// A client identifier secret would follow this, but there is no secret, so it is the empty string.
final clientCredentials = Base64Encoder().convert("$clientID:".codeUnits);
final response = await _http.post("$host/auth/token",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic $clientCredentials"
},
body: body);
print(response.body);
final Map<String,dynamic> jsonMap = json.decode(response.body as String) as Map<String, dynamic>;
window.localStorage.addAll({
"access_token": jsonMap["access_token"] as String,
"token_type": jsonMap["token_type"] as String,
"expires_in": (jsonMap["expires_in"] as int).toString()
});
}
}
......@@ -15,8 +15,8 @@ import 'main.template.dart' as self;
// Import 'package:http/browser_client.dlart' and change the above to:
// ClassProvider(Client, useClass: BrowserClient),
])
InjectorFactory injector = self.injector$Injector as InjectorFactory;
InjectorFactory injector = self.injector$Injector;
void main() {
runApp(ng.AppComponentNgFactory as ComponentFactory, createInjector: injector);
runApp(ng.AppComponentNgFactory, createInjector: injector);
}
......@@ -61,22 +61,20 @@ class CalenderChannel extends ApplicationChannel {
router
.route('/auth/token')
.link(() => AuthController(authServer));
router
.route('/appointments/[:id]')
.link(() => Authorizer.bearer(authServer))
.link(() => AppointmentController(context));
router
.route('/appointments/lookup/[:year/[:month/[:day]]]')
.link(() => Authorizer.bearer(authServer))
.link(() => AppointmentController(context));
/*
router
.route('/user/[:number]')
.link(() => UserController(context));
router
.route('/contacts/[:number]')
.link(() => ContactController(context));
*/
return router;
}
......
import 'dart:async';
import 'dart:convert';
import 'package:aqueduct/aqueduct.dart';
import 'package:middleman/model/person.dart';
import 'package:http/http.dart' as http; // Must include http package in your pubspec.yaml
class RegisterController extends ResourceController {
RegisterController(this.context, this.authServer);
......
......@@ -6,6 +6,8 @@ author: stable|kernel <jobs@stablekernel.com>
environment:
sdk: ">=2.0.0 <3.0.0"
dependencies:
aqueduct: ^3.0.0
http: ^0.12.0+2
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment