Retrieve Device and Login details of User in AngularJS

Last Reply 14 days ago By skp

Posted 19 days ago

How to get the device details such as Mobile model, mobile name, ip address and the browser used to operate the application and also to get the username from the login form with logged in time and logged out time in angularjs?

Posted 14 days ago

 

var n = navigator;
Agent = n.userAgent;
browserName = n.appName;
appVersion = n.appVersion.substr(0, n.appVersion.indexOf(' '));
fullVersion = '' + parseFloat(n.appVersion);
platform = n.platform;
cookie = n.cookieEnabled;
BrMajorVersion = parseInt(n.appVersion, 10);
OSname = 'unknownOS';
width = (screen.width) ? screen.width : '';
height = (screen.height) ? screen.height : '';
screensize = '';
screensize += '' + width + " x " + height;

var mobile = {
    Android: function () {
        return Agent.match(/Android/i);
    },
    BlackBerry: function () {
        return Agent.match(/BlackBerry/i);
    },
    iOS: function () {
        return Agent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function () {
        return Agent.match(/Opera Mini/i);
    },
    Windows: function () {
        return Agent.match(/IEMobile/i);
    },
    any: function () {
        return (mobile.Android() || mobile.BlackBerry() || mobile.iOS() || mobile.Opera() || mobile.Windows());
    }
};

// In Chrome
if ((objOffsetVersion = Agent.indexOf("Chrome")) != -1) {
    browserName = "Chrome";
    fullVersion = Agent.substring(objOffsetVersion + 7);
}
    // In Microsoft internet explorer
else if ((objOffsetVersion = Agent.indexOf("MSIE")) != -1) {
    browserName = "Microsoft Internet Explorer";
    fullVersion = Agent.substring(objOffsetVersion + 5);
}
    // In Firefox
else if ((objOffsetVersion = Agent.indexOf("Firefox")) != -1) {
    browserName = "Firefox";
}
    // In Safari
else if ((objOffsetVersion = Agent.indexOf("Safari")) != -1) {
    browserName = "Safari";
    fullVersion = Agent.substring(objOffsetVersion + 7);
    if ((objOffsetVersion = Agent.indexOf("Version")) != -1) fullVersion = Agent.substring(objOffsetVersion + 8);
}
    // For other browser "name/version" is at the end of userAgent
else if ((OffsetName = Agent.lastIndexOf(' ') + 1) < (objOffsetVersion = Agent.lastIndexOf('/'))) {
    browserName = Agent.substring(OffsetName, objOffsetVersion);
    fullVersion = Agent.substring(objOffsetVersion + 1);
    if (browserName.toLowerCase() == browserName.toUpperCase()) {
        browserName = navigator.appName;
    }
}
// trimming the fullVersion string at semicolon/space if present
if ((ix = fullVersion.indexOf(";")) != -1) fullVersion = fullVersion.substring(0, ix);
if ((ix = fullVersion.indexOf(" ")) != -1) fullVersion = fullVersion.substring(0, ix);
BrMajorVersion = parseInt('' + fullVersion, 10);
if (isNaN(BrMajorVersion)) {
    fullVersion = '' + parseFloat(navigator.appVersion);
    BrMajorVersion = parseInt(navigator.appVersion, 10);
}

//OS
if (appVersion.indexOf("Win") != -1) {
    OSname = 'windowsOS';
}
if (appVersion.indexOf("Mac") != -1) {
    OSname = 'macOS';
}
if (appVersion.indexOf("X11") != -1) {
    OSname = 'unixOS';
}
if (appVersion.indexOf("Linux") != -1) {
    OSname = 'linuxOS';
}

angular.module('myApp').
controller('dashboardController', function ($scope, $http, $sessionStorage, $window, $location, useragent) {
 $scope.device = useragent.device();
    $scope.browserName = useragent.browser();
    $scope.platform = useragent.platform();
    $scope.cookie = useragent.cookie();
    $scope.version = useragent.version();
    $scope.versiontrue = useragent.version(true);
    $scope.OS = useragent.OS();
    $scope.appVersion = useragent.appVersion();
    $scope.screen = useragent.screen();
    $http.get("https://ipinfo.io/json").then(function (response) {
        $scope.ip = response.data.ip;       
    });    
}).factory('useragent', function () {
    return {
        browser: function () {
            return browserName;
        },
        device: function () {
            return mobile.any() ? mobile.any().toString() : 'Desktop';
        },
        platform: function () {
            return platform;
        },
        version: function (v) {
            return v ? fullVersion : BrMajorVersion;
        },
        OS: function () {
            return OSname;
        },
        cookie: function () {
            return cookie;
        },
        appVersion: function () {
            return appVersion;
        },
        screen: function () {
            return screensize;
        }
    }
});