Commit 8e182a4c authored by Mauritz, Falk Marius's avatar Mauritz, Falk Marius
Browse files

Auth in Client

todo
parent 60a0be9e
Pipeline #6482 passed with stage
in 3 minutes and 36 seconds
......@@ -38,6 +38,10 @@ ALTER USER cal_user WITH password 'password';
CREATE DATABASE calendar;
GRANT all ON database calendar TO cal_user;
----------------------------------------------
aqueduct auth add-client --id com.calendar.app
// dockercompose
......
......@@ -14,11 +14,8 @@ import '../main_component.dart';
styleUrls: ['login_component.css'],
directives: [coreDirectives, routerDirectives, formDirectives],
)
class LoginComponent implements OnInit, OnActivate {
LoginComponent(this._router, this._userService,this._registerService);
LoginComponent(this._router, this._userService, this._registerService);
final UserService _userService;
final RegisterService _registerService;
......@@ -30,21 +27,17 @@ class LoginComponent implements OnInit, OnActivate {
String username;
String password;
List<User> users;
//User user;
//
//
Future<void> login() async {
await _registerService.login(username, password);
loggedIn = true;
AppComponent.showButtons = true;
await _registerService.login(username,password);
await _router.navigate('/dashboard');
}
// Weiterleitung zum Registrierungsformular
Future<void> register() async {
await _router.navigate('/register');
......@@ -68,8 +61,8 @@ class LoginComponent implements OnInit, OnActivate {
@override
void onActivate(RouterState previous, RouterState current) {
if(loggedIn){
if (loggedIn) {
_router.navigate('/calendar');
}
}
}
\ No newline at end of file
}
......@@ -36,10 +36,8 @@ class AppointmentService {
// Add security Header
final Response response = await _http.put(_appointmentUrl,
headers: _headers, body: json.encode(appointment.toJson())) as Response;
if(response.statusCode == 200){
return Appointment.fromJson(
_extractData(response as Response) as Map<String, dynamic>);
}
}
///Löscht den Termin mit gegebener id
......@@ -51,7 +49,7 @@ class AppointmentService {
Future<Appointment> get(int id) async {
final Response response =
await _http.get('$_appointmentUrl/$id') as Response;
await _http.get('$_appointmentUrl/$id',headers: _headers) as Response;
return Appointment.fromJson(_extractData(response) as Map<String, dynamic>);
}
......
import 'dart:async';
import 'dart:convert';
import 'dart:html';
import 'package:demo/src/model/person.dart';
import 'package:demo/src/view/main_component.dart';
import 'package:http/http.dart';
......@@ -8,7 +9,13 @@ class ContactService {
// request to middlemand
ContactService(this._http);
static final _headers = {'Content-Type': 'application/json'};
// getter for the Authorization
Map<String, String> get _headers => Map.fromEntries(
[MapEntry("Authorization", "$_tokenType $_tokenAuth"), _type]);
String get _tokenType => window.localStorage["token_type"];
String get _tokenAuth => window.localStorage["access_token"];
static const _type = MapEntry('Content-Type', 'application/json');
static const _userUrl = host + '/user'; // URL to web API
static const _contactUrl = host + '/contacts'; // URL to web API
final Client _http;
......@@ -26,7 +33,7 @@ class ContactService {
final url = '$_contactUrl/${contact.contactCode}';
final response =
await _http.put(url, headers: _headers, body: json.encode(contact));
return Contact.fromJson(_extractData(response) as Map<String, dynamic>);
return Contact.fromJson(_extractData(response as Response) as Map<String, dynamic>);
} catch (e) {
throw _handleError(e);
}
......@@ -34,7 +41,7 @@ class ContactService {
///gibt eine Liste von allen Terminen zurück
Future<List<Contact>> getAll() async {
final Response response = await _http.get('$_contactUrl');
final Response response = (await _http.get('$_contactUrl')) as Response;
return (_extractData(response) as List)
.map((value) => Contact.fromJson(value as Map<String, dynamic>))
.toList();
......@@ -42,7 +49,7 @@ class ContactService {
///Gibt den Termin mit der gegebenen id zurück
Future<Contact> get(String contactCode) async {
final Response response = await _http.get('$_contactUrl/$contactCode');
final Response response = (await _http.get('$_contactUrl/$contactCode')) as Response;
return Contact.fromJson(_extractData(response) as Map<String, dynamic>);
}
......@@ -51,13 +58,12 @@ class ContactService {
try {
final response = await _http.post(_contactUrl,
headers: _headers, body: json.encode(contact));
return Contact.fromJson(_extractData(response) as Map<String, dynamic>);
return Contact.fromJson(_extractData(response as Response) as Map<String, dynamic>);
} catch (e) {
throw _handleError(e);
}
}
Future<void> delete(String contactCode) async {
try {
final url = '$_contactUrl/$contactCode';
......@@ -69,8 +75,10 @@ class ContactService {
///Erstellt einen Kontakt und gibt ihn zurück
Future<Contact> find(String contactCode) async {
final Response response = await _http.get('$_userUrl/$contactCode');
final User user = User.fromJson(_extractData(response) as Map<String, dynamic>);
return Contact(user.id, user.username, user.surname,user.name, user.email, user.contactCode, "");
final Response response = (await _http.get('$_userUrl/$contactCode')) as Response;
final User user =
User.fromJson(_extractData(response) as Map<String, dynamic>);
return Contact(user.id, user.username, user.surname, user.name, user.email,
user.contactCode, "");
}
}
......@@ -49,6 +49,7 @@ class RegisterService {
body: body);
final Map<String,dynamic> jsonMap = json.decode(response.body as String) as Map<String, dynamic>;
capitalize(String s) => s[0].toUpperCase() + s.substring(1);
print(jsonMap.toString());
window.localStorage.addAll({
"access_token": jsonMap["access_token"] as String,
"token_type": capitalize(jsonMap["token_type"] as String),
......
......@@ -78,6 +78,7 @@ class AppointmentController extends ResourceController {
@Operation.put()
Future<Response> updateAppointment() async {
final Map<String, dynamic> body = await request.body.decode();
logger.warning(request.body.decode());
final query = Query<Appointment>(context)
..values.read(body,ignore: ["id"])
..where((app) => app.id).equalTo(body["id"] as int);
......
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