Login/logout, Firestore rules, readme

This commit is contained in:
Norbi Peti 2020-12-15 00:29:45 +01:00
parent 9ae5c9e9a6
commit b3291391ff
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
8 changed files with 116 additions and 51 deletions

View file

@ -1,27 +1,36 @@
# Szakdolgozat
Egy webalkalmazás, amely nyomonköveti egy-egy kurzus követelményeinek teljesitését oktatók és hallgatók számára.
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 11.0.2.
## Szerepkörök
Csak bejelentkezett felhasználók férhetnek hozzá bármilyen adathoz. A saját adataikat mindig tudják módositani.
## Development server
### Admin
* Teljes jogosultsága van az adatokhoz, kivéve a felhasználók adatait
* Hozzá tud rendelni más felhasználókat szerepkörökhöz egy-egy kurzus kapcsán
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
### Hallgató
* Az adott kurzushoz tartozó adatokat csak megtekinteni tudja
## Code scaffolding
### Oktató
* Az adott kurzushoz tartozó összes adatot tudja módositani, kivéve a nevet, azonositót.
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.
## Adatok
### Kurzus
* Azonositó
* Név
* Leirás
* Követelmények
* Eredmények
* Oktatók
* Hallgatók
## Build
### Követelmény
* ...
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.
### Eredmény
* ...
## Running unit tests
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).
## Running end-to-end tests
Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).
## Further help
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page.
### Felhasználó
* Azonositó
* E-mail, jelszó (külön tárolva)
* Admin-e

View file

@ -1,9 +1,24 @@
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if
request.time < timestamp.date(2021, 1, 10);
function sameUser(user) {
return request.auth != null && request.auth.uid == user;
}
function getUserData() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data;
}
//Felhasználói adatok kezelése
match /users/{user} {
allow create: if sameUser(user) && request.auth.uid == request.resource.data.author_uid;
allow get, list, update, delete: if sameUser(user) && request.auth.uid == resource.data.author_uid;
}
//Adminoknak mindent lehet
match /data/{document=**} {
allow get, list, create, update, delete: if getUserData().isAdmin == true;
}
//Diákok megnézhetik a kurzus adatait
match /data/courses/{course} {
allow get, list: if request.auth.uid in resource.data.students;
}
}
}
}

View file

@ -14,7 +14,7 @@
<mat-toolbar color="primary">
<button
type="button"
aria-label="Toggle sidenav"
aria-label="Oldalsáv"
mat-icon-button
(click)="drawer.toggle()"
*ngIf="isHandset$ | async">
@ -22,11 +22,16 @@
</button>
<span>Szakdolgozat</span>
<span class="toolbar-spacer"></span>
<a mat-button routerLink="/register">Regisztráció</a>
<a mat-button routerLink="/login">
Bejelentkezés
<mat-icon aria-hidden="false" aria-label="Bejelentkezés">login</mat-icon>
</a>
<span *ngIf="loginService.loggedInUser">
<a mat-button (click)="logout()">Kijelentkezés</a>
</span>
<span *ngIf="!loginService.loggedInUser">
<a mat-button routerLink="/register">Regisztráció</a>
<a mat-button routerLink="/login">
Bejelentkezés
<mat-icon aria-hidden="false" aria-label="Bejelentkezés">login</mat-icon>
</a>
</span>
</mat-toolbar>
<div style="margin: 20px">
<!-- <mat-slider min="1" max="100" step="1" value="1"></mat-slider> -->

View file

@ -1,15 +1,17 @@
import { Component } from '@angular/core';
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { Observable } from 'rxjs';
import { map, shareReplay } from 'rxjs/operators';
import {Component, OnInit} from '@angular/core';
import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';
import {Observable} from 'rxjs';
import {map, shareReplay} from 'rxjs/operators';
import {LoginService} from './shared/login.service';
import firebase from 'firebase';
import {Router} from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
export class AppComponent implements OnInit {
isHandset$: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.Handset)
.pipe(
@ -17,13 +19,15 @@ export class AppComponent {
shareReplay()
);
constructor(private breakpointObserver: BreakpointObserver) {}
}
firebase.initializeApp((window as any).firebaseCredentials);
firebase.auth().onAuthStateChanged(user => {
if (user) {
constructor(private breakpointObserver: BreakpointObserver, public loginService: LoginService,
private router: Router) {
}
});
ngOnInit(): void {
}
async logout(): Promise<void> {
await this.loginService.logout();
await this.router.navigate(['/']);
}
}

View file

@ -3,13 +3,13 @@
<mat-form-field>
<mat-label>Email</mat-label>
<input matInput class="mat-input-element" type="email" name="email" required="required"
placeholder="h123456@stud.u-szeged.hu"/>
placeholder="h123456@stud.u-szeged.hu" [(ngModel)]="email"/>
<mat-hint>Egyetemi email cim</mat-hint>
</mat-form-field>
<mat-form-field>
<mat-label>Jelszó</mat-label>
<input matInput class="mat-input-element" type="password" name="password" required="required"
minlength="8"/>
minlength="8" [(ngModel)]="pass"/>
</mat-form-field>
<button mat-raised-button color="primary" (click)="doLogin()">Bejelentkezés</button>
</form>

View file

@ -1,4 +1,6 @@
import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {LoginService} from '../shared/login.service';
@Component({
selector: 'app-login',
@ -6,10 +8,18 @@ import {Component, OnInit} from '@angular/core';
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
email: string;
pass: string;
constructor(private router: Router, private loginService: LoginService) {
}
ngOnInit(): void {
}
doLogin(): void {
async doLogin(): Promise<void> {
await this.loginService.login(this.email, this.pass);
await this.router.navigate(['/']);
}
}

View file

@ -2,6 +2,8 @@ import {Component, OnInit} from '@angular/core';
import {ErrorStateMatcher} from '@angular/material/core';
import {AbstractControl, FormControl, FormGroupDirective, NgForm, ValidationErrors, Validators} from '@angular/forms';
import firebase from 'firebase';
import {LoginService} from '../shared/login.service';
import {Router} from '@angular/router';
export class FormErrorStateMatcher implements ErrorStateMatcher {
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
@ -17,7 +19,7 @@ export class FormErrorStateMatcher implements ErrorStateMatcher {
})
export class RegisterComponent implements OnInit {
constructor() {
constructor(private loginService: LoginService, private router: Router) {
}
emailFormControl = new FormControl('', [
@ -73,8 +75,8 @@ export class RegisterComponent implements OnInit {
return;
}
try {
const user = await firebase.auth().createUserWithEmailAndPassword(this.emailFormControl.value, this.passFormControl.value);
alert('User created: ' + user);
await this.loginService.createUser(this.emailFormControl.value, this.passFormControl.value);
await this.router.navigate(['/']);
} catch (e) {
alert(e);
}

View file

@ -1,9 +1,29 @@
import { Injectable } from '@angular/core';
import {Injectable} from '@angular/core';
import firebase from 'firebase';
@Injectable({
providedIn: 'root'
})
export class LoginService {
constructor() { }
loggedInUser: firebase.User;
constructor() {
firebase.initializeApp((window as any).firebaseCredentials);
firebase.auth().onAuthStateChanged(user => {
this.loggedInUser = user;
});
}
async createUser(email: string, pass: string): Promise<void> {
const user = await firebase.auth().createUserWithEmailAndPassword(email, pass);
}
async logout(): Promise<void> {
await firebase.auth().signOut();
}
async login(email: string, pass: string): Promise<void> {
await firebase.auth().signInWithEmailAndPassword(email, pass);
}
}