我必须处理登录用户的身份验证和授权。
我在Angular中创建了一个注册表单,为此,我在注册时使用python flask创建了一个后端API,注册数据将保存到表中,之后,我必须进行登录用户身份验证,授权,我不知道如何进行。
我正在分享我的注册代码。
HTML:
<form (ngSubmit)="onSubmit(register)" #register="ngForm">
<mat-form-field class="example-full-width">
<input matInput placeholder="First Name" name="first_name" [(ngModel)]="first_name" required>
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="Last Name" name="last_name" [(ngModel)]="last_name" required>
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="Email ID" name = "email" [(ngModel)]= "email" required>
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="Mobile Number" name = "mobile_number" [(ngModel)]= "mobile_number" required>
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="Password" name = "password" [(ngModel)]= "password" required >
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="Confirm Password" name = "confirm_password" [(ngModel)]= "confirm_password" required>
</mat-form-field>
<button type="submit" class="btn float-center btn-sm shadow btnSForm" >Submit</button>
</form>
Python烧瓶:
from flask_login import UserMixin
from APIs import db, ma
# In this class we are declaring the table columns to be created in the table.
class Registration(db.Model):
reg_id = db.Column(db.Integer, primary_key=True)
usercred_id = db.relationship('UserCredentials', backref='registration', uselist=False)
first_name = db.Column(db.String(32), unique=False, nullable=False)
last_name = db.Column(db.String(32), unique=False, nullable=False)
email = db.Column(db.String(255), unique=True, nullable=False)
mobile_number = db.Column(db.BIGINT, unique=False, nullable=True)
password = db.Column(db.String(255), unique=False, nullable=False)
class UserCredentials(db.Model, UserMixin):
usercred_id = db.Column(db.Integer, primary_key=True, nullable=False)
email = db.Column(db.String(255), unique=True, nullable=False)
password = db.Column(db.String(255), unique=False, nullable=False)
industry_id = db.Column(db.Integer, unique=False, nullable=True)
subindustry_id = db.Column(db.Integer, unique=False, nullable=True)
reg_id = db.Column(db.Integer, db.ForeignKey('registration.reg_id'), nullable=False)
# This class is the sub-class for converting the data into json format, this class is using marshmallow.
class Schema(ma.ModelSchema):
class Meta:
model = Registration
def addRegister(register_data, user_data):
try:
reg = Registration(**register_data)
usr = UserCredentials(**user_data, registration=reg)
db.session.add(reg, usr)
db.session.commit()
return True
except Exception:
return False
finally:
db.session.close()
观看次数:
@app.route("/registration", methods=["GET", "POST"])
def post():
if request.method == 'POST':
save_data = request.get_json()
try:
return jsonify(addRegister(save_data))
except Exception:
return jsonify({'Unsuccessful': 'Looks like you missed something'})