mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
|
|
const authProvider = require('./authProvider.js')
|
||
|
|
const db = require('../config/db.js')
|
||
|
|
const { ObjectId } = require('mongodb');
|
||
|
|
|
||
|
|
|
||
|
|
class AuthUserAssociation {
|
||
|
|
constructor(authProviderId, authId, userId) {
|
||
|
|
this._id = new ObjectId();
|
||
|
|
this.authProvider_id = authProviderId;
|
||
|
|
this.auth_id = authId;
|
||
|
|
this.user_id = userId;
|
||
|
|
this.connected = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
async find_user_association(provider_name,auth_id){
|
||
|
|
await db.connect()
|
||
|
|
const conn = db.getConnection();
|
||
|
|
|
||
|
|
const collection = conn.collection('authUserAssociation');
|
||
|
|
const provider_id = await authProvider.getId(provider_name)
|
||
|
|
|
||
|
|
const userAssociation = await collection.findOne({ authProvider_id: provider_id,auth_id,auth_id });
|
||
|
|
return userAssociation
|
||
|
|
}
|
||
|
|
|
||
|
|
async link(provider_name,auth_id,user_id){
|
||
|
|
await db.connect()
|
||
|
|
const conn = db.getConnection();
|
||
|
|
|
||
|
|
const collection = conn.collection('authUserAssociation');
|
||
|
|
const provider_id = await authProvider.getId(provider_name)
|
||
|
|
|
||
|
|
const userAssociation = await collection.findOne({ authProvider_id: provider_id, user_id: user_id });
|
||
|
|
|
||
|
|
if(!userAssociation){
|
||
|
|
return await collection.insertOne({
|
||
|
|
_id:ObjectId,
|
||
|
|
authProvider_id:provider_id,
|
||
|
|
auth_id:auth_id,
|
||
|
|
user_id:user_id,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async unlink(provider_name,user_id){
|
||
|
|
await db.connect()
|
||
|
|
const conn = db.getConnection();
|
||
|
|
|
||
|
|
const collection = conn.collection('authUserAssociation');
|
||
|
|
const provider_id = await authProvider.getId(provider_name)
|
||
|
|
|
||
|
|
const userAssociation = await collection.findOne({ authProvider_id: provider_id, user_id: user_id });
|
||
|
|
|
||
|
|
if(userAssociation){
|
||
|
|
return await collection.deleteOne(userAssociation)
|
||
|
|
} else return null
|
||
|
|
}
|
||
|
|
}
|
||
|
|
module.exports = new AuthUserAssociation;
|