18.05.2016 Views

Mittwoch, 18. Mai, 2016

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

MongoDB CRUD the MVC way with Passport Authentication 68<br />

35 return this.password === md5;<br />

36 };<br />

37<br />

38 UserSchema.statics.findUniqueUsername = function(username, suffix, callback) {<br />

39 var _this = this;<br />

40 var possibleUsername = username + (suffix || '');<br />

41<br />

42 _this.findOne(<br />

43 {username: possibleUsername},<br />

44 function(err, user) {<br />

45 if (!err) {<br />

46 if (!user) {<br />

47 callback(possibleUsername);<br />

48 }<br />

49 else {<br />

50 return _this.findUniqueUsername(username, (suffix || 0) + 1,\<br />

51 callback);<br />

52 }<br />

53 }<br />

54 else {<br />

55 callback(null);<br />

56 }<br />

57 }<br />

58 );<br />

59 };<br />

60<br />

61 mongoose.model('User', UserSchema);<br />

In this mumbo jumbo you’ve added three fields to your UserSchema object:<br />

• provider property - the strategy used to register the user<br />

• providerId property - the user identifier for the authentication strategy<br />

• providerData property - you’ll use it later to store the user object retrieved from OAuth<br />

providers

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!