aboutsummaryrefslogtreecommitdiff
path: root/src/server/authentication/DashUserModel.ts
diff options
context:
space:
mode:
authorBob Zeleznik <zzzman@gmail.com>2020-05-14 16:33:44 -0400
committerBob Zeleznik <zzzman@gmail.com>2020-05-14 16:33:44 -0400
commitb9440e34d89b28acacdd6eed2cda39cd2a1d8c46 (patch)
tree9d7c91a4f6ebf7270bbc66180cadbb592dac40a7 /src/server/authentication/DashUserModel.ts
parentf6a55c0d613787f60e4a40b728a1acbc6c39c552 (diff)
parente31ec0a7d4fe9772f1e52081717e53e0fdf9fb9b (diff)
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web
Diffstat (limited to 'src/server/authentication/DashUserModel.ts')
-rw-r--r--src/server/authentication/DashUserModel.ts86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/server/authentication/DashUserModel.ts b/src/server/authentication/DashUserModel.ts
new file mode 100644
index 000000000..51d920a8f
--- /dev/null
+++ b/src/server/authentication/DashUserModel.ts
@@ -0,0 +1,86 @@
+//@ts-ignore
+import * as bcrypt from "bcrypt-nodejs";
+//@ts-ignore
+import * as mongoose from 'mongoose';
+
+export type DashUserModel = mongoose.Document & {
+ email: String,
+ password: string,
+ passwordResetToken?: string,
+ passwordResetExpires?: Date,
+
+ userDocumentId: string;
+
+ profile: {
+ name: string,
+ gender: string,
+ location: string,
+ website: string,
+ picture: string
+ },
+
+ comparePassword: comparePasswordFunction,
+};
+
+type comparePasswordFunction = (candidatePassword: string, cb: (err: any, isMatch: any) => {}) => void;
+
+export type AuthToken = {
+ accessToken: string,
+ kind: string
+};
+
+const userSchema = new mongoose.Schema({
+ email: String,
+ password: String,
+ passwordResetToken: String,
+ passwordResetExpires: Date,
+
+ userDocumentId: String,
+
+ facebook: String,
+ twitter: String,
+ google: String,
+
+ profile: {
+ name: String,
+ gender: String,
+ location: String,
+ website: String,
+ picture: String
+ }
+}, { timestamps: true });
+
+/**
+ * Password hash middleware.
+ */
+userSchema.pre("save", function save(next) {
+ const user = this as DashUserModel;
+ if (!user.isModified("password")) {
+ return next();
+ }
+ bcrypt.genSalt(10, (err: any, salt: string) => {
+ if (err) {
+ return next(err);
+ }
+ bcrypt.hash(user.password, salt, () => void {}, (err: mongoose.Error, hash: string) => {
+ if (err) {
+ return next(err);
+ }
+ user.password = hash;
+ next();
+ });
+ });
+});
+
+const comparePassword: comparePasswordFunction = function (this: DashUserModel, candidatePassword, cb) {
+ // Choose one of the following bodies for authentication logic.
+ // secure (expected, default)
+ bcrypt.compare(candidatePassword, this.password, cb);
+ // bypass password (debugging)
+ // cb(undefined, true);
+};
+
+userSchema.methods.comparePassword = comparePassword;
+
+const User = mongoose.model("User", userSchema);
+export default User; \ No newline at end of file