WUserServer

WUserServer

new WUserServer(optopt) → {Object}

Description:
  • 建立使用者hapi伺服器

Source:
Example
import WUserServer from 'WUserServer/src/w-user-server.mjs'


//webName
let webName = 'User management system'
// let webName = '使用者管理系統'


//webUrl
let webUrl = 'https://google.com'


//webDescription
let webDescription = "Let's do something and make it easier"
// let webDescription = '讓我們一起做更簡單的事'


//orm
let orm = WOrmMongodb({
    url: 'mongodb://username:password@127.0.0.1:27017',
    db: 'wuser',
    cl: 'users',
})


//opt
let opt = {
    orm,
    port: 8080,
    apiName: 'api',
    authUrl: 'http://localhost:8080/auth', //need webUrl+'/'+auth, use 'http://localhost:8080' in test case
    salt: 'zLAUfSLDUuausd0Aasu912SDU', //generate it for sites
    timeTokenExp: 30,

    emSenderName: webName,
    emSenderEmail: "sender's email", //email address for email sender
    emSenderPW: "sender's password", //password for email sender

    webName,
    webUrl,
    webDescription,

    // emLetterTeamMessage: `${webName}開發團隊 敬上`,
    // emLetterDoNotReplayMessage: '本信由系統自動發信,請勿回信',
    emLetterLinks: [{ name: 'Google', url: 'https://google.com' }],

    // emSignUpTitle: '註冊驗證信',
    // emSignUpHtml: `<p>親愛的 {name} 您好:</p><p>已收到您的註冊申請,請點擊下方連結進行驗證,確定驗證成功後即可登入。</p><p><a href="{urlCheckCode}" target="_blank">點擊此處驗證</a></p>`,

    // emResetPWTitle: '重設密碼驗證信',
    // emResetPWHtml: `<p>親愛的 {name} 您好:</p><p>已收到您的重設密碼申請,請點擊下方連結進行驗證,確定驗證成功後即可登入。</p><p><a href="{urlCheckCode}" target="_blank">點擊此處驗證</a></p>`,

    // verifyWebMsgSuccess: '驗證成功',
    // verifyWebMsgExpired: '驗證失敗',
    // verifyWebMsgCount: '{s} 秒後自動前往網站',

    funcs: {},
    routes: [],
}


//new
let wus = new WUserServer(opt)
wus.on('all', function({ eventName, data }) {
    console.log('all:' + eventName, ...data)
})
Parameters:
Name Type Attributes Default Description
opt Object <optional>
{}

輸入設定物件,預設{}

Properties
Name Type Attributes Default Description
orm Object <optional>
{}

輸入操作資料庫物件,預設{}

serverHapi Object <optional>
{}

輸入hapi伺服器物件,若提供,本服務將自動加入api至route。使用外部hapi伺服器時,需開啟跨域功能,或是使用nginx反向代理轉入api請求

port Integer <optional>
8080

輸入Hapi伺服器所在port整數,預設8080

apiName String <optional>
'api'

輸入Hapi伺服器api接口名稱字串,預設'api'

authUrl String <optional>
'http://localhost:8080/auth'

輸入Hapi伺服器外部連入驗證網址字串,末端api路徑需為auth,預設'http://localhost:8080/auth'

salt String <optional>
''

輸入密碼加密用鹽字串,預設''

timeTokenExp Integer <optional>
30

輸入token過期時間整數,預設30(分鐘)

webName String <optional>
''

輸入email模板內網站名稱字串,預設''

webUrl String <optional>
''

輸入email模板內網站外部網址首頁字串,預設''

webDescription String <optional>
''

輸入email模板內網站簡易描述字串,預設''

emSenderName String <optional>
'WUserServer'

輸入寄件人顯示名稱字串,預設'WUserServer'

emSenderEmail String

輸入寄件人email信箱字串

emSenderPW String <optional>
''

輸入寄件人email密碼字串,預設''

emBCC String <optional>
''

輸入寄信備份用之密件收件人email字串,預設''

emLetterHtml String <optional>
'file:emLetterHtml.mjs'

輸入email模板字串,大括號標注字串為系統取代字串,預設模板文字放於'emLetterHtml.mjs'

emLetterTeamMessage String <optional>
'Global-User International Company'

輸入email模板內標注團隊字串,預設'Global-User International Company'

emLetterDoNotReplayMessage String <optional>
'This letter is automatically sent by the system, please do not reply to this email.'

輸入email模板內請勿回覆字串,預設'This letter is automatically sent by the system, please do not reply to this email.'

emLetterLinks Array <optional>
[]

輸入email模板內超連結資訊物件,超連結物件需給予name與url兩屬性,預設[]

emSignUpTitle String <optional>
'Verification letter'

輸入註冊驗證email標題字串,預設'Verification letter'

emSignUpHtml String <optional>
'file:emSignUpHtml.mjs'

輸入註冊驗證email html模板字串,大括號標注字串為系統取代字串,預設模版文字放於'emSignUpHtml.mjs'

emResetPWTitle String <optional>
'Password reset letter'

輸入重設密碼驗證email標題字串,預設'Password reset letter'

emResetPWHtml String <optional>
'file:emResetPWHtml.mjs'

輸入重設密碼驗證email html模板字串,大括號標注字串為系統取代字串,預設模版文字放於'emResetPWHtml.mjs'

verifyWebHtml String <optional>
'file:verifyWebHtml.mjs'

輸入驗證連結網站html模板字串,大括號標注字串為系統取代字串,預設模版文字放於'verifyWebHtml.mjs'

verifyWebLogo String <optional>
'file:verifyWebLogo.mjs'

輸入驗證連結網站用logo圖字串,預設圖字串放於'verifyWebLogo.mjs',為svg圖字串

verifyWebMsgSuccess String <optional>
'Verify successfully'

輸入驗證連結網站當成功驗證時之顯示字串,預設'Verify successfully'

verifyWebMsgExpired String <optional>
'Code has expired'

輸入驗證連結網站當無法驗證時之顯示字串,預設'Code has expired'

verifyWebMsgCount String <optional>
'Go to the website automatically after {s} seconds...'

輸入驗證連結網站當成功驗證時之倒數文字字串,預設'Go to the website automatically after {s} seconds..Promise.'

funcs Object <optional>
{}

輸入擴充指定執行函數物件,預設{}

routes Array <optional>
[]

輸入擴充hapi routes陣列,預設[]

Returns:

回傳通訊物件

Type
Object

Methods

(static) changePW(user) → {Promise}

Description:
  • 使用者變更密碼

    1.帳號需為有效(active為activeYes)

    2.帳號需驗證後才能更改密碼

Source:
Parameters:
Name Type Description
user Object

輸入使用者物件

Properties
Name Type Description
email String

輸入email字串

pwEnc String

輸入密碼加密後字串

Returns:

回傳Promise,resolve回傳物件,成功則state為'success',失敗則state為'error',於msg顯示錯誤訊息,無reject

Type
Promise

(static) isValidToken(token) → {Promise}

Description:
  • 確認使用者token是否有效

    1.確認使用者token是否仍於有效時限內

Source:
Parameters:
Name Type Description
token String

輸入使用者token字串

Returns:

回傳Promise,resolve回傳物件,成功則state為'success',失敗則state為'error',於msg顯示錯誤訊息,無reject

Type
Promise

(static) logIn(user) → {Promise}

Description:
  • 使用者登入

    1.使用者以email與密碼為登入,且帳號需為有效(active為activeYes)

    2.帳號需驗證後才能登入,使用者需先收信驗證

    3.無驗證則重寄驗證信。若使用者忘記點擊或沒收到信,則該email不能再被申請,需由使用者用忘記密碼功能,系統重新寄送驗證信後進行驗證

    4.忘記密碼則重寄驗證信。若使用者登入失敗或忘記密碼,請使用者確認自己email是否正確。若是忘記密碼,則使用忘記密碼功能,系統重新寄送驗證信後進行驗證

    5.支援多點登入。若使用者重複或多點登入時,且token亦於有效時限內,則直接給予當前token而不再另外產生,避免造成前次token失效

Source:
Parameters:
Name Type Description
user Object

輸入使用者物件

Properties
Name Type Description
email String

輸入email字串

pwEnc String

輸入密碼加密後字串

Returns:

回傳Promise,resolve回傳物件,成功則state為'success',於msg顯示使用者token,失敗則state為'error',於msg顯示錯誤訊息,無reject

Type
Promise

(static) logOut(token) → {Promise}

Description:
  • 使用者登出

    1.使用者憑token登出

Source:
Parameters:
Name Type Description
token String

輸入使用者token字串

Returns:

回傳Promise,resolve回傳物件,成功則state為'success',失敗則state為'error',於msg顯示錯誤訊息,無reject

Type
Promise

(static) modifyInfor(inp) → {Promise}

Description:
  • 使用者更改自己資料

Source:
Parameters:
Name Type Description
inp Object

輸入token與查詢物件

Properties
Name Type Attributes Default Description
token String

輸入使用者token字串

user Object <optional>
{}

輸入使用者資訊物件,預設{}

Returns:

回傳Promise,resolve回傳物件,成功則state為'success',失敗則state為'error',於msg顯示錯誤訊息,無reject

Type
Promise

(static) refreshTokenExp(token) → {Promise}

Description:
  • 更新使用者token過期時間

    1.token需仍於有效時限內才能刷新

Source:
Parameters:
Name Type Description
token String

輸入使用者token字串

Returns:

回傳Promise,resolve回傳物件,成功則state為'success',失敗則state為'error',於msg顯示錯誤訊息,無reject

Type
Promise

(static) resetPW(user) → {Promise}

Description:
  • 使用者重設密碼

    1.帳號需為有效(active為activeYes)

    2.使用者忘記密碼時,可直接設定新密碼並寄送驗證信

    2.帳號不需通過驗證也可申請變更密碼,當使用者申請帳號但忘記收信驗證時,以此可重新設定新密碼並收信驗證

Source:
Parameters:
Name Type Description
user Object

輸入使用者物件

Properties
Name Type Description
email String

輸入email字串

pwEncNew String

輸入新密碼加密後字串

Returns:

回傳Promise,resolve回傳物件,成功則state為'success',失敗則state為'error',於msg顯示錯誤訊息,無reject

Type
Promise

(static) select(inp) → {Promise}

Description:
  • 公開用查詢使用者

Source:
Parameters:
Name Type Description
inp Object

輸入token與查詢物件

Properties
Name Type Attributes Default Description
token String

輸入使用者token字串

find Object <optional>
{}

輸入查詢物件,預設{}

Returns:

回傳Promise,resolve回傳物件,成功則state為'success',於msg為使用者資料陣列,失敗則state為'error',於msg顯示錯誤訊息,無reject

Type
Promise

(static) signUp(user) → {Promise}

Description:
  • 使用者註冊新帳號

    1.使用者用email註冊帳號,且email於全系統唯一不得重複,已申請過不得再被使用

    2.註冊之後,使用者需收信驗證通過,帳號才會有效(active為activeYes)

    3.若使用者忘記點擊或沒收到信,則該email不能再被申請,需由使用者用忘記密碼功能,系統重新寄送驗證信後進行驗證

    4.被停權者(active非activeYes),需由系統管理員復權

Source:
Parameters:
Name Type Description
user Object

輸入使用者物件

Properties
Name Type Attributes Default Description
email String

輸入email字串

pwEnc String

輸入密碼加密後字串

address String <optional>
''

輸入地址字串

phone String <optional>
''

輸入電話字串

organization String <optional>
''

輸入組織字串

position String <optional>
''

輸入職位字串

Returns:

回傳Promise,resolve回傳物件,成功則state為'success',於msg顯示使用者id,失敗則state為'error',於msg顯示錯誤訊息,無reject

Type
Promise

(inner) defaultProp(u) → {Object}

Description:
  • 針對使用者資料物件處理,若無給指定欄位,則自動給予預設值

Source:
Parameters:
Name Type Description
u Object

輸入使用者資料物件

Returns:

回傳使用者資料物件,指定欄位若沒給則自動填入預設值

Type
Object

(inner) sendEmail(emTitle, emContent, toEmails, toEmailsCCopt, toEmailsBCCopt) → {Promise}

Description:
  • 寄送email

Source:
Parameters:
Name Type Attributes Default Description
emTitle String

輸入郵件名稱字串

emContent String

輸入郵件訊息字串

toEmails String | Array

輸入收件人email字串或陣列

toEmailsCC String | Array <optional>
[]

輸入副本收件人email字串或陣列,預設[]

toEmailsBCC String | Array <optional>
[]

輸入密件副本收件人email字串或陣列,預設[]

Returns:

回傳Promise,resolve回傳成功訊息,reject回傳錯誤訊息

Type
Promise

(inner) sendEmailByLetter(emTitle, emContent, toEmails, toEmailsCCopt, toEmailsBCCopt) → {Promise}

Description:
  • 使用模板呈現內文並寄送email

Source:
Parameters:
Name Type Attributes Default Description
emTitle String

輸入郵件名稱字串

emContent String

輸入郵件訊息字串

toEmails String | Array

輸入收件人email字串或陣列

toEmailsCC String | Array <optional>
[]

輸入副本收件人email字串或陣列,預設[]

toEmailsBCC String | Array <optional>
[]

輸入密件副本收件人email字串或陣列,預設[]

Returns:

回傳Promise,resolve回傳成功訊息,reject回傳錯誤訊息

Type
Promise

(inner) verifyProp(u) → {Object}

Description:
  • 驗證使用者資料欄位

Source:
Parameters:
Name Type Description
u Object

輸入使用者資料物件

Returns:

回傳判斷物件,state為'success'為成功驗證,state為'error'為有錯誤,msg為儲存錯誤訊息

Type
Object