
No Comments Yet
Be the first to share your thoughts and start the conversation.
Be the first to share your thoughts and start the conversation.
Complete source code available till this point of lesson is available at
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
In this lesson, we learn how to create an account model using TypeScript and Mongoose. This model allows users to authenticate through multiple providers while maintaining organized user information.
00:00:02 Let me show you how we can create an account model.
00:00:05 Head over to Database and create a new file called account.model.ts.
00:00:11 And before I start creating it, let's ask ourselves, why do we need an account in the first place?
00:00:17 And how does it differ from the user model?
00:00:19 Well, take a look at the differences or the properties that the account has.
00:00:24 It has things like the user ID, name, image, and password, but what matters the most is the provider and the provider account ID.
00:00:32 Think of the account as one version of this specific user.
00:00:36 A user can be authenticated, see, authenticated through the account, for each one of the providers on its own.
00:00:44 like we can have a single user called John Doe, and then they can have multiple accounts through different providers.
00:00:51 One can be Google, the other one can be email and password, and the third one can be GitHub.
00:00:56 This allows users to connect through multiple login options while keeping their primary user information organized and separated.
00:01:05 Nothing but the production level database structure here.
00:01:07 So let's go ahead and create it.
00:01:09 One more time, I will do it by doing everything from scratch.
00:01:13 And then in the future, we can just copy and paste the old model structure and then modify what we need to modify.
00:01:19 So let's get started by creating a new account schema and make it equal to a new schema coming from Mongoose to which we need to pass different properties
00:01:31 that define this schema, such as the user ID, which is going to be equal to type, which is equal to schema.types.objectId,
00:01:42 and it'll be a reference to the user.
00:01:45 So we can say ref to user.
00:01:48 And it will be a required because we must have a user in order to have its user account.
00:01:55 Next, we can also give it a name and this will be of a type string required to true.
00:02:02 We can give it an image, which is going to be just a type string.
00:02:05 We don't have to make it required in case it's an email and password auth.
00:02:10 We can have a password attached to this user account, and again, don't make it required because for GitHub or Google Auth,
00:02:18 it's passwordless.
00:02:20 Let's also provide a provider, and provider will be required.
00:02:25 As well as the provider account ID, which is going to be equal to type string and require it to true.
00:02:32 Exactly the properties that we have right here.
00:02:34 Now, of course, you can create a type for this account schema.
00:02:38 That's going to look something like this.
00:02:40 Export interface, I account, and then you can define all those properties.
00:02:46 Like user ID is not going to be a string.
00:02:50 Rather, it'll be a types coming from mongoose.objectid.
00:02:56 Next, we have a name of string, image, which is optional of a type string, password, also optional of a type string, provider is going to be a string,
00:03:07 but you could also make it an enum, which could mean Google, Facebook, GitHub, et cetera.
00:03:12 And then the provider account ID, which is a string.
00:03:16 And now we can say that the schema has to follow this specific iAccount interface.
00:03:22 Finally, we also need to say that we're going to turn the timestamps on, or rather to true.
00:03:29 And we can create a model of the account based on the account schema by saying if models coming from Mongoose question mark that account already exists,
00:03:41 then we'll use that.
00:03:42 Else we'll create a new model of the type I account called account based on the account schema.
00:03:49 And we can export default that account.
00:03:53 And that's it.
00:03:54 You've just created your second model, which is attached to a user because each user has a specific account.
00:04:02 Let's go ahead and commit it by saying implement account model, commit and sync.
00:04:11 Great work.