2013-08-20 09:43:18 +08:00
|
|
|
fs = require("fs")
|
|
|
|
sanitizer = require("sanitizer")
|
|
|
|
util = require("util")
|
|
|
|
|
|
|
|
config = require("../config")
|
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
moduleDeps = ["App"]
|
2013-09-16 07:10:19 +08:00
|
|
|
|
2013-08-20 10:51:26 +08:00
|
|
|
# The main router that registers the routes that can be accessed by the client.
|
2013-08-20 09:43:18 +08:00
|
|
|
module.exports = class MainRouter
|
2013-09-16 07:10:19 +08:00
|
|
|
|
|
|
|
constructor: () ->
|
|
|
|
config.modules.wait moduleDeps, =>
|
|
|
|
@app = config.modules.get("App")
|
2014-04-24 04:18:25 +08:00
|
|
|
# @redisAction = config.modules.get("RedisAction")
|
|
|
|
# @redisStore = config.modules.get("RedisStore")
|
2013-09-16 07:10:19 +08:00
|
|
|
@_registerRoutes()
|
2013-08-20 09:43:18 +08:00
|
|
|
|
|
|
|
_registerRoutes: () ->
|
|
|
|
@app.get "/", @_index
|
2014-04-24 04:18:25 +08:00
|
|
|
# @app.get "/auth", @_getAuth
|
|
|
|
# @app.post "/auth", @_postAuth
|
|
|
|
# @app.post "/logout", @_requiresLogin, @_logout
|
|
|
|
# @app.get "/meetings", @_meetings
|
2013-08-20 09:43:18 +08:00
|
|
|
|
|
|
|
# When requesting the homepage a potential meetingID and sessionID are extracted
|
|
|
|
# from the user's cookie. If they match with a user that is in the database under
|
|
|
|
# the same data, they are instantly redirected to join into the meeting.
|
|
|
|
# If they are not, they will be redirected to the index page where they can enter
|
|
|
|
# their login details.
|
2013-10-29 01:23:15 +08:00
|
|
|
#
|
|
|
|
# This method is registered as a route on express.
|
|
|
|
#
|
|
|
|
# @internal
|
2013-09-16 07:10:19 +08:00
|
|
|
_index: (req, res) =>
|
2014-04-24 04:18:25 +08:00
|
|
|
# @redisAction.getMeetings (err, meetings) ->
|
|
|
|
res.render "index",
|
|
|
|
title: config.appName
|
|
|
|
meetings: []
|
2013-08-20 09:43:18 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
# # Upon submitting their login details from the index page via a POST request, a meeting
|
|
|
|
# # will be created and joined. If an error occurs, which usually results in using an
|
|
|
|
# # invalid username or meetingID, the user receives an error response. Both success and
|
|
|
|
# # error responses are in json only.
|
|
|
|
# #
|
|
|
|
# # This method is registered as a route on express.
|
|
|
|
# #
|
|
|
|
# # @internal
|
|
|
|
# _postAuth: (req, res) =>
|
|
|
|
# user = req.body
|
|
|
|
# username = user.username = sanitizer.escape(user.username)
|
|
|
|
# meetingID = user.meetingID = sanitizer.escape(user.meetingID)
|
|
|
|
# sessionID = req.sessionID
|
2013-10-29 01:23:15 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
# validParameters = @_validateLoginParameters username, meetingID
|
2013-10-29 01:23:15 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
# if validParameters
|
|
|
|
# @redisAction.makeMeeting meetingID, sessionID, username, (result) ->
|
|
|
|
# user.loginAccepted = result
|
|
|
|
# # save the ids so socketio can get the username and meeting
|
|
|
|
# if result
|
|
|
|
# res.cookie "sessionid", sessionID
|
|
|
|
# res.cookie "meetingid", meetingID
|
|
|
|
# res.contentType "json"
|
|
|
|
# res.send(user)
|
|
|
|
# else
|
|
|
|
# user.loginAccepted = false
|
|
|
|
# res.send(user)
|
2013-08-20 09:43:18 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
# # Returns a json informing if there's an authenticated user or not. The meetingID and
|
|
|
|
# # sessionID are extracted from the user's cookie. If they match with a user that is
|
|
|
|
# # in the database, the user is accepted and his information is included in the response.
|
|
|
|
# # If they don't match, the user is not accepted.
|
|
|
|
# #
|
|
|
|
# # This method is registered as a route on express.
|
|
|
|
# #
|
|
|
|
# # @internal
|
|
|
|
# _getAuth: (req, res) =>
|
|
|
|
# @redisAction.isValidSession req.cookies["meetingid"], req.cookies["sessionid"], (err, valid) ->
|
|
|
|
# res.contentType "json"
|
|
|
|
# user = {}
|
|
|
|
# unless valid
|
|
|
|
# user.loginAccepted = false
|
|
|
|
# res.send user
|
|
|
|
# else
|
|
|
|
# user.loginAccepted = true
|
|
|
|
# user.meetingID = req.cookies.meetingid
|
|
|
|
# # user.username = ?? // TODO
|
|
|
|
# res.send user
|
2013-08-20 09:43:18 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
# # When a user logs out, their session is destroyed and their cookies are cleared.
|
|
|
|
# # @param {Object} req Request object from the client
|
|
|
|
# # @param {Object} res Response object to the client
|
|
|
|
# #
|
|
|
|
# # This method is registered as a route on express.
|
|
|
|
# #
|
|
|
|
# # @internal
|
|
|
|
# _logout: (req, res) =>
|
|
|
|
# req.session.destroy() # end the session
|
|
|
|
# res.cookie "sessionid", null # clear the cookie from the client
|
|
|
|
# res.cookie "meetingid", null
|
|
|
|
# res.redirect "/"
|
2013-08-20 09:43:18 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
# # @param {Object} req Request object from the client
|
|
|
|
# # @param {Object} res Response object to the client
|
|
|
|
# #
|
|
|
|
# # This method is registered as a route on express.
|
|
|
|
# #
|
|
|
|
# # @internal
|
|
|
|
# _meetings: (req, res) =>
|
|
|
|
# @redisAction.getMeetings (err, results) ->
|
|
|
|
# res.contentType "json"
|
|
|
|
# res.send JSON.stringify(results)
|
2013-08-20 09:43:18 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
# # If a page requires authentication to view, this function is used to verify that there
|
|
|
|
# # is a user logged in.
|
|
|
|
# # @param {Object} req Request object from client
|
|
|
|
# # @param {Object} res Response object to client
|
|
|
|
# # @param {Function} next To be run as a callback if valid
|
|
|
|
# #
|
|
|
|
# # This method is registered as a route on express.
|
|
|
|
# #
|
|
|
|
# # @internal
|
|
|
|
# _requiresLogin: (req, res, next) =>
|
|
|
|
# # check that they have a cookie with valid session id
|
|
|
|
# @redisAction.isValidSession req.cookies["meetingid"], req.cookies["sessionid"], (err, isValid) ->
|
|
|
|
# if isValid
|
|
|
|
# next()
|
|
|
|
# else
|
|
|
|
# res.redirect "/"
|
2013-08-20 09:43:18 +08:00
|
|
|
|
2014-04-24 04:18:25 +08:00
|
|
|
# # Checks whether the parameters passed by the user to login are correct
|
|
|
|
# # @param username [string] the username passed by the user
|
|
|
|
# # @param meetingID [string] the meetingID passed by the user
|
|
|
|
# # @return [boolean] whether the parameters are correct or not
|
|
|
|
# # @internal
|
|
|
|
# _validateLoginParameters: (username, meetingID) ->
|
|
|
|
# username? and meetingID? and
|
|
|
|
# username.length <= config.maxUsernameLength and
|
|
|
|
# meetingID.split(" ").length is 1
|